PDA

View Full Version : Segmentation error while using Structure with readBlock()



ashukla
2nd September 2007, 13:39
Dear everyone!
the code is as follows;

struct txtsc
{
QString text1;
QColor fg,bg;
QFont Font;
}t12;

int dlgTextScroller::pbOpen_clicked()
{
QString file=QFileDialog::getOpenFileName("/home/anurag/MCP/textscroller/","All Files (*)", this, "TextScroller Open File dialg","Select a file") ;
struct txtsc t;
leFileName->setText(file);
//read data
ifstream inf(file,ios::in|ios::binary);
if(!inf)
{

cout<<"Cann't Open read File";
return 1;
}

inf.read((char *)&t,sizeof(struct txtsc));
txtScrollEdit->setText(t.text1);
inf.close();
return 0;
}

it gives segmentation fault.

marcel
2nd September 2007, 13:53
Of course you're it is not working...
You can't just read a sequence of bytes from a file and expect to get a QString or a QColor out of it.

I suppose you try to serialize the data structure.
There are easier ways:
- Use a QDataStream not an STL stream( not mandatory)
- For the QString write only QString::toAscii().constData() ( this means const char*);
- For the QColor write only the RGB components. This means 3 int's;
- For the QFont write the font family and optionally the font style, weight, etc...

Writing this way will make it easier to construct the structure's members upon loading the file.

Regar

jpn
2nd September 2007, 21:42
Or provide data stream operators (http://doc.trolltech.com/latest/qmetatype.html#qRegisterMetaTypeStreamOperators). ;)

ashukla
3rd September 2007, 10:36
Or provide data stream operators (http://doc.trolltech.com/latest/qmetatype.html#qRegisterMetaTypeStreamOperators). ;)
Dear Sir!
Plz give me a small example for this.
Hoping an earlier response.

jpn
3rd September 2007, 12:13
Something like this at simplest:


QDataStream & operator<<(QDataStream &stream, const txtsc &data)
{
stream << data.text1;
stream << data.fg;
stream << data.bg;
stream << data.Font;
return stream;
}

QDataStream & operator>>(QDataStream &stream, txtsc &data)
{
stream >> data.text1;
stream >> data.fg;
stream >> data.bg;
stream >> data.Font;
return stream;
}


Now you can read/write a whole txtsc struct with QDataStream. See docs for example code.

ashukla
3rd September 2007, 12:45
Something like this at simplest:


QDataStream & operator<<(QDataStream &stream, const txtsc &data)
{
stream << data.text1;
stream << data.fg;
stream << data.bg;
stream << data.Font;
return stream;
}

QDataStream & operator>>(QDataStream &stream, txtsc &data)
{
stream >> data.text1;
stream >> data.fg;
stream >> data.bg;
stream >> data.Font;
return stream;
}


Now you can read/write a whole txtsc struct with QDataStream. See docs for example code.

Dear Sir!
This code is well.
but I have done it previously in following way.

int dlgTextScroller::pbOpen_clicked()
{
QString ofile=QFileDialog::getOpenFileName("/home/anurag/MCP/textscroller/","All Files (*)", this, "TextScroller Open File dialog","Select a file") ;
QString text,back,fore,tmpfont;
QFile file(ofile);
file.open( IO_ReadOnly );
QDataStream stream( &file ); // we will serialize the data into the file
stream>>text; // serialize a string
stream>>back; // serialize an integer
stream>>fore;
stream>>tmpfont;

//Testing Code
std::cout<<fore;
std::cout<<"Back Color"<<back<<endl;
//stream<<n;

txtScrollEdit->setText(text);
txtScrollEdit->setFont(tmpfont);
txtScrollEdit->setPaletteForegroundColor(fore);
txtScrollEdit->setPaletteBackgroundColor(back);
file.close();
return 0;
}
I have done it.
but I want to use readblock function to read Structure or Class (Block Size) data using file.
How a way I get the source code of readBlock() function to refinement the functionality of this? Give some guidelines for me.

jpn
3rd September 2007, 19:58
May I ask, why do you insist of using readBlock() while you can have:


txtsc t;
stream >> t;

?

ashukla
6th September 2007, 06:01
May I ask, why do you insist of using readBlock() while you can have:


txtsc t;
stream >> t;

?
Dear Sir!
I am not insisting. I have already complete my function with stream; but I want to know that any way is possible with readBlock().
I want to know readBlock() function purpose; if it cann't support Struct variable or class object. What's the purpose to create this.
In the case of pure C++ read() & write() function work well; but in the Qt environment it doesn't work.

marcel
6th September 2007, 06:20
It is mostly used to read small chunks from larger files. Needed when large files need to be processed.

You can also write structs, but ones that have members of basic types( int, float, char[],etc).

Regards

ashukla
6th September 2007, 08:13
It is mostly used to read small chunks from larger files. Needed when large files need to be processed.

You can also write structs, but ones that have members of basic types( int, float, char[],etc).

Regards
Thanks Sir!