PDA

View Full Version : How to port fread in qt4 ?



darshan.hardas
15th October 2007, 07:37
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?

marcel
15th October 2007, 08:06
You could do it with QIODevice::read:


file.read((const char*)&structObj, sizeof(A));


But it is better to use a QDataStream and read all members one by one:


..open file
QDataStream stream(&file);
struct A structObj;
stream >> structObj.number; //int member
stream >> structObj.string; //a const char*
..etc


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

ChristianEhrlicher
15th October 2007, 08:06
Plz take a look at QFile (http://doc.trolltech.com/4.3/qfile)

darshan.hardas
15th October 2007, 08:56
I have a struct which doesn't have a string object.

I'm trying :

QResource qResource(filePath);
char str[sizeof(A)*count];
QDataStream qDataStream ((char*)qResource.data());
int byteRead = qDataStream.readRawData(str, sizeof(A)*count)


It is not giving me any error :p
Is this correct? :crying:

marcel
15th October 2007, 09:00
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.

darshan.hardas
15th October 2007, 09:05
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

qResource->isCompressed();

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

marcel
15th October 2007, 09:10
When accessing the data through a QFile the uncompression is done automatically. See the QResource documentation.

darshan.hardas
15th October 2007, 09:15
But How do I check whether the data file is compressed or not?
Also whether the file is valid or not? :crying:

-----------------------------------------------------------------------------
Darshan :)

marcel
15th October 2007, 09:22
You don't have to check for compression.
When you use something like this:


QFile file(":/someFile");

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.

darshan.hardas
15th October 2007, 09:25
I'm setting some flag if the data is compressed. else returning other value

darshan.hardas
15th October 2007, 10:41
I have a struct INT_FILE_HEADER_DATA

struct INTF_DATA_FILE_HEADER {
int FileId;
short VersionNr;
DGEO_POINT NECrn; //DGEO_POINT is a class
DGEO_POINT SWCrn;
int NorthPoints;
int EastPoints;
};


And I need to port the following


if ( fread(&m_rPlotDataHeader,sizeof(INTF_DATA_FILE_HEADER),1, fp) == 0 ) {
if ( bIsCompressed )
pclose(fp);
else
fclose(fp);
return 0;
}


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

Please help to port this code

marcel
15th October 2007, 20:55
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.

darshan.hardas
16th October 2007, 13:27
I'm not compressing any file. i just want to read a file. If the file is compressed I'm setting one variable.

marcel
16th October 2007, 13:36
Goddamn it!
I already explained you: you don't care whether the file is compressed or not if you read it with QFile.

darshan.hardas
16th October 2007, 13:48
then how do i set that variable