PDA

View Full Version : c structure & Qfile



eva2002
21st January 2010, 02:43
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.



typedef struct user {
Qstring user;
QString pass;
} user;




QFile mapfile(filename);
mapfile.open(QIODevice::ReadWrite);
//how to proceed from here?

nikhilqt
21st January 2010, 05:23
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



mapFile.write(" your data inside it ");
mapFile.close();

eva2002
21st January 2010, 05:50
Thanks for the reply.

However, I want to dump the whole struct into the file.

aamer4yu
21st January 2010, 06:55
Have a look at QDataStream

eva2002
22nd January 2010, 00:02
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?

Ginsengelf
22nd January 2010, 08:26
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

eva2002
23rd January 2010, 23:43
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.

wysota
24th January 2010, 00:04
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...

eva2002
24th January 2010, 07:09
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.



struct place {
QString placename;
QString x;
QString y;
};

struct group {
QString grpname;
QList<place> building;
};

struct mapinfo {
QString map_path;
QList<group> grp;
};


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.

wysota
24th January 2010, 10:11
Why don't you use an SQLite database for it? It seems perfect for your needs.


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.
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).

squidge
24th January 2010, 10:39
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.
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).

wysota
24th January 2010, 11:09
Instead of using structures, use classes

In C++ a structure is a class as well. Only that its default scope is public and not protected.

faldzip
24th January 2010, 11:46
Or use XML to store your data as it is tree structure which suits XML.

squidge
24th January 2010, 11:55
In C++ a structure is a class as well. Only that its default scope is public and not protected.Bad choice of words on my part. I never use structures in C++ as it seems too 'C'ish. I only use them in my embedded applications which are all C.

eva2002
25th January 2010, 02:47
I look through some example of sql. I wonder if it is possible to save the database?

faldzip
25th January 2010, 09:37
I look through some example of sql. I wonder if it is possible to save the database?
SQLite is a file-based database so it is already save in a file as the whole database is this one file.