Results 1 to 15 of 15

Thread: How to port fread in qt4 ?

  1. #1
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to port fread in qt4 ?

    I want to port fread in Qt 4.3.2.

    Im reading a compressed file.
    zcat file.Z
    I have replaced it with QResource.

    Earlier we were having
    FILE* pFile = popen(filePath, "r");
    Now I want to read from this file using "fread" and store the byte in some struct*
    The statement to be ported is:
    struct A structObj;
    fread(&structObj, sizeof(A), 1, pFile);

    Can anyone help me?

  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: How to port fread in qt4 ?

    You could do it with QIODevice::read:
    Qt Code:
    1. file.read((const char*)&structObj, sizeof(A));
    To copy to clipboard, switch view to plain text mode 

    But it is better to use a QDataStream and read all members one by one:
    Qt Code:
    1. ..open file
    2. QDataStream stream(&file);
    3. struct A structObj;
    4. stream >> structObj.number; //int member
    5. stream >> structObj.string; //a const char*
    6. ..etc
    To copy to clipboard, switch view to plain text mode 

    Note that this code is not tested, it is meant to be just an example.

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

    darshan.hardas (15th October 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to port fread in qt4 ?

    Plz take a look at QFile

  5. The following user says thank you to ChristianEhrlicher for this useful post:

    darshan.hardas (15th October 2007)

  6. #4
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: How to port fread in qt4 ?

    I have a struct which doesn't have a string object.

    I'm trying :
    Qt Code:
    1. QResource qResource(filePath);
    2. char str[sizeof(A)*count];
    3. QDataStream qDataStream ((char*)qResource.data());
    4. int byteRead = qDataStream.readRawData(str, sizeof(A)*count)
    To copy to clipboard, switch view to plain text mode 

    It is not giving me any error
    Is this correct?

  7. #5
    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: How to port fread in qt4 ?

    What if you have more than one file in the resource?
    It is better to use QFile to open the required file from the resource and then you can use the code you already have.

  8. #6
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: How to port fread in qt4 ?

    Do you mean should I use QFile instead of QResource
    But this resource file which I'm using is a compressed one
    so I'm using
    Qt Code:
    1. qResource->isCompressed();
    To copy to clipboard, switch view to plain text mode 

    -----------------------------------------------------------------
    Darshan

  9. #7
    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: How to port fread in qt4 ?

    When accessing the data through a QFile the uncompression is done automatically. See the QResource documentation.

  10. #8
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: How to port fread in qt4 ?

    But How do I check whether the data file is compressed or not?
    Also whether the file is valid or not?

    -----------------------------------------------------------------------------
    Darshan

  11. #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: How to port fread in qt4 ?

    You don't have to check for compression.
    When you use something like this:
    Qt Code:
    1. QFile file([COLOR=#000000][FONT='Courier New,courier']":/someFile");[/FONT][/COLOR]
    To copy to clipboard, switch view to plain text mode 
    and read data form the file with a QDataStream or whatever, the data comes uncompressed.

    Only if the file itself was compressed before you actually add it to the resource, then you have to uncompress it manually.

  12. #10
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: How to port fread in qt4 ?

    I'm setting some flag if the data is compressed. else returning other value
    Darshan

  13. #11
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: How to port fread in qt4 ?

    I have a struct INT_FILE_HEADER_DATA
    Qt Code:
    1. struct INTF_DATA_FILE_HEADER {
    2. int FileId;
    3. short VersionNr;
    4. DGEO_POINT NECrn; //DGEO_POINT is a class
    5. DGEO_POINT SWCrn;
    6. int NorthPoints;
    7. int EastPoints;
    8. };
    To copy to clipboard, switch view to plain text mode 

    And I need to port the following

    Qt Code:
    1. if ( fread(&m_rPlotDataHeader,sizeof(INTF_DATA_FILE_HEADER),1,fp) == 0 ) {
    2. if ( bIsCompressed )
    3. pclose(fp);
    4. else
    5. fclose(fp);
    6. return 0;
    7. }
    To copy to clipboard, switch view to plain text mode 

    bIsCompressed is a bool object. This object is set to "true" when file is compressed

    Please help to port this code
    Darshan

  14. #12
    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: How to port fread in qt4 ?

    How do you know when the file is compressed? Kist by asking QResource?
    You must understand that resource files can be accessed with QFile just as normal files.
    QResource also compresses files, but accessing the files with QFile makes uncompression an automatic operation from the user's point of view, meaning that you don't care whether the file is compressed or uncompressed. Qt decompresses it automatically for you.

    So you can read your struct as you already did in post #4 but this time using a QFile, because as I already told you, you might have multiple files stored in the resource.

    If you are positive that you will always have only one file in the resource then you can go with your solution.

  15. #13
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Arrow Re: How to port fread in qt4 ?

    I'm not compressing any file. i just want to read a file. If the file is compressed I'm setting one variable.
    Darshan

  16. #14
    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: How to port fread in qt4 ?

    Goddamn it!
    I already explained you: you don't care whether the file is compressed or not if you read it with QFile.

  17. #15
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Angry Re: How to port fread in qt4 ?

    then how do i set that variable
    Darshan

Similar Threads

  1. how to control 32 com port through application
    By amit_pansuria in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 06:59
  2. serial port communiction
    By jagadish in forum Qt Programming
    Replies: 4
    Last Post: 7th July 2007, 12:04
  3. First attempt to display serial port data on GUI
    By ShaChris23 in forum Newbie
    Replies: 12
    Last Post: 4th May 2007, 09:14
  4. Replies: 6
    Last Post: 18th April 2007, 15:04
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.