Results 1 to 10 of 10

Thread: Segmentation error while using Structure with readBlock()

  1. #1
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Segmentation error while using Structure with readBlock()

    Dear everyone!
    the code is as follows;

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

    int dlgTextScroller:bOpen_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.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation error while using Structure with readBlock()

    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

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Segmentation error while using Structure with readBlock()

    Or provide data stream operators.
    J-P Nurmi

  4. #4
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Exclamation Re: Segmentation error while using Structure with readBlock()

    Quote Originally Posted by jpn View Post
    Dear Sir!
    Plz give me a small example for this.
    Hoping an earlier response.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Segmentation error while using Structure with readBlock()

    Something like this at simplest:
    Qt Code:
    1. QDataStream & operator<<(QDataStream &stream, const txtsc &data)
    2. {
    3. stream << data.text1;
    4. stream << data.fg;
    5. stream << data.bg;
    6. stream << data.Font;
    7. return stream;
    8. }
    9.  
    10. QDataStream & operator>>(QDataStream &stream, txtsc &data)
    11. {
    12. stream >> data.text1;
    13. stream >> data.fg;
    14. stream >> data.bg;
    15. stream >> data.Font;
    16. return stream;
    17. }
    To copy to clipboard, switch view to plain text mode 

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

  6. The following user says thank you to jpn for this useful post:

    ashukla (3rd September 2007)

  7. #6
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation error while using Structure with readBlock()

    Quote Originally Posted by jpn View Post
    Something like this at simplest:
    Qt Code:
    1. QDataStream & operator<<(QDataStream &stream, const txtsc &data)
    2. {
    3. stream << data.text1;
    4. stream << data.fg;
    5. stream << data.bg;
    6. stream << data.Font;
    7. return stream;
    8. }
    9.  
    10. QDataStream & operator>>(QDataStream &stream, txtsc &data)
    11. {
    12. stream >> data.text1;
    13. stream >> data.fg;
    14. stream >> data.bg;
    15. stream >> data.Font;
    16. return stream;
    17. }
    To copy to clipboard, switch view to plain text mode 

    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:bOpen_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.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Segmentation error while using Structure with readBlock()

    May I ask, why do you insist of using readBlock() while you can have:
    Qt Code:
    1. txtsc t;
    2. stream >> t;
    To copy to clipboard, switch view to plain text mode 
    ?
    J-P Nurmi

  9. #8
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation error while using Structure with readBlock()

    Quote Originally Posted by jpn View Post
    May I ask, why do you insist of using readBlock() while you can have:
    Qt Code:
    1. txtsc t;
    2. stream >> t;
    To copy to clipboard, switch view to plain text mode 
    ?
    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.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  10. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation error while using Structure with readBlock()

    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

  11. The following user says thank you to marcel for this useful post:

    ashukla (6th September 2007)

  12. #10
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Thumbs up Re: Segmentation error while using Structure with readBlock()

    Quote Originally Posted by marcel View Post
    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!
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.