Results 1 to 16 of 16

Thread: c structure & Qfile

  1. #1
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default c structure & Qfile

    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.

    Qt Code:
    1. typedef struct user {
    2. Qstring user;
    3. QString pass;
    4. } user;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QFile mapfile(filename);
    2. mapfile.open(QIODevice::ReadWrite);
    3. //how to proceed from here?
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: c structure & Qfile

    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

    Qt Code:
    1. mapFile.write(" your data inside it ");
    2. mapFile.close();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: c structure & Qfile

    Thanks for the reply.

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

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: c structure & Qfile

    Have a look at QDataStream

  5. #5
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: c structure & Qfile

    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?

  6. #6
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c structure & Qfile

    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

  7. #7
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: c structure & Qfile

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: c structure & Qfile

    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...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: c structure & Qfile

    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.

    Qt Code:
    1. struct place {
    2. QString placename;
    3. };
    4.  
    5. struct group {
    6. QString grpname;
    7. QList<place> building;
    8. };
    9.  
    10. struct mapinfo {
    11. QString map_path;
    12. QList<group> grp;
    13. };
    To copy to clipboard, switch view to plain text mode 

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: c structure & Qfile

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: c structure & Qfile

    Quote Originally Posted by eva2002 View Post
    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).

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: c structure & Qfile

    Quote Originally Posted by fatjuicymole View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c structure & Qfile

    Or use XML to store your data as it is tree structure which suits XML.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  14. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: c structure & Qfile

    Quote Originally Posted by wysota View Post
    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.

  15. #15
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: c structure & Qfile

    I look through some example of sql. I wonder if it is possible to save the database?

  16. #16
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c structure & Qfile

    Quote Originally Posted by eva2002 View Post
    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.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Tree structure
    By ikm in forum Newbie
    Replies: 1
    Last Post: 7th August 2009, 20:19
  2. Folder structure of Qt SDK
    By piotr.dobrogost in forum Installation and Deployment
    Replies: 19
    Last Post: 14th July 2009, 09:17
  3. Treeview Structure
    By bismitapadhy in forum Qt Programming
    Replies: 2
    Last Post: 3rd June 2009, 13:44
  4. Data Structure help!
    By darshan in forum Newbie
    Replies: 8
    Last Post: 18th February 2009, 12:47
  5. Structure and pointers
    By zorro68 in forum General Programming
    Replies: 3
    Last Post: 22nd October 2007, 14:38

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.