Hi all,
Anyone knows how to write a c struct into Qfile and extract them?
below is a sample struct to be written to a file.
Printable View
what exactly you want ? Do you want to write the contents of the struct inside the file OR do you want to simply dump the struct format inside the file.
Anyways writing inside,
Check whether the file is opened for you, by checking the return status of the mapFile.open(...) and after
Code:
mapFile.write(" your data inside it "); mapFile.close();
Thanks for the reply.
However, I want to dump the whole struct into the file.
Have a look at QDataStream
I have tried QDatastream but I am using the << and >> operator. It return me errors.
Is there any examples which I can follow to get it to work?
Hi, could you show some code and the errors you get?
In the documentation for QDataStream is a short example how to use it to write to a file.
Ginsengelf
the error i got is
error: no match for ‘operator<<’ in ‘in << grp’
the error occur since they do not know the type of my struct.
First of all it's not a "C struct" anymore because it contains a C++ object inside (so the typedef is pointless and you can discard it). Second of all if you want to use QDataStream with your class, you need to implement redirection operators for it. Third of all using QDataStream will not make a dump of the structure to the file because QDataStream is a serialization mechanism that stores some additional data. Lastly, what exactly do you want to achieve? I mean the bigger picture...
I have this set of records which I am trying to place into a file so I can retrieve it at a later date.
I generally have 3 structures (shown below). it contains all the info of my application. I was trying to write the whole mapinfo struct into a file.
Code:
In c FILE function, fread and fwrite could place the whole struct into a file using the following code:
fread(&map, sizeof(struct mapinfo), 1, mapfile);
fwrite(&map, sizeof(struct mapinfo), 1, mapfile);
where,
*FILE mapfile;
mapinfo map
I tried doing this with my application but it failed. keep getting seg fault.
Why don't you use an SQLite database for it? It seems perfect for your needs.
This has no chance of working because you are using objects in your structures. You would copy *something* to the file and it would work but there would be no way of retrieving the data back (especially that you wouldn't save the data this way as it is kept separate from the "main" object).Quote:
In c FILE function, fread and fwrite could place the whole struct into a file using the following code:
fread(&map, sizeof(struct mapinfo), 1, mapfile);
fwrite(&map, sizeof(struct mapinfo), 1, mapfile);
where,
*FILE mapfile;
mapinfo map
I tried doing this with my application but it failed. keep getting seg fault.
Instead of using structures, use classes, and then provide the << and >> operators for that class which handle the objects in the class. If you don't want to use operators, provide a Serialise() method instead. That way the storing and loading of the object data is encapsulated all within the class itself, rather than spread throughout your code. It then becomes much easier to use that object elsewhere (even other projects).
Or use XML to store your data as it is tree structure which suits XML.
I look through some example of sql. I wonder if it is possible to save the database?