Results 1 to 6 of 6

Thread: structures to a file (read and write)

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default structures to a file (read and write)

    Guys,

    Sample I have structure

    Qt Code:
    1. struct s_mystruct
    2. {
    3. int noOfCars;
    4. double noOfWives;
    5. bool isActive;
    6. }mystruct;
    7.  
    8. mystruct st;
    9. st.noOfCars = 10;
    10. st.noOfWives = 2.5;
    11. st.isActive = true;
    To copy to clipboard, switch view to plain text mode 

    I want to write this to a file (text format ) like this
    NoOfCars = 1
    noOfWives = 2.5
    isActive = true
    Then, read the txt file and fill my structure again.
    Question:
    Is there any easy way to do this taking advantage of some "QtClasses" ?

    baray98

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: structures to a file (read and write)

    Take a look at QSettings:
    Qt Code:
    1. QSettings settings("/path/to/settings.ini", QSettings::IniFormat);
    2.  
    3. // write
    4. settings.setValue("NoOfCars", st.noOfCars);
    5. ...
    6.  
    7. // read
    8. st.noOfCars = settings.value("NoOfCars").toInt();
    9. ...
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    baray98 (10th February 2008)

  4. #3
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: structures to a file (read and write)

    thanks for the reply ...just a follow up .. I have encountered the following

    from my practice I did the following

    Qt Code:
    1. typedef struct s_mystruct
    2. {
    3. int car;
    4. double wife;
    5. bool isActive;
    6. QHash <int,QString> map;
    7. }t_mystruct;
    8.  
    9. t_mystruct st;
    10.  
    11. QSettings settings("information.xfg",QSettings::IniFormat);
    12.  
    13. for (int i = 0; i <20;i++) //just to try
    14. {
    15. st.map.insert(i,QString("Driver No. %1").arg(i));
    16. }
    17.  
    18. settings.beginGroup("CarMap");
    19. settings.beginWriteArray("Car");
    20. QList<int> keys = st.map.keys();
    21. for (int i = 0; i < keys.count(); ++i) {
    22. settings.setArrayIndex(i);
    23. int xx = keys.at(i);
    24. settings.setValue("PlateNumber", keys.at(i));
    25. settings.setValue("Driver",st.map.value(keys.at(i)));
    26. }
    27. settings.endArray();
    28. settings.endGroup();
    To copy to clipboard, switch view to plain text mode 

    then, i got


    [CarMap]
    Car\1\Driver=Driver No. 0
    Car\1\PlateNumber=0
    Car\10\Driver=Driver No. 9
    Car\10\PlateNumber=9
    Car\11\Driver=Driver No. 10
    Car\11\PlateNumber=10
    Car\12\Driver=Driver No. 11
    Car\12\PlateNumber=11
    Car\13\Driver=Driver No. 12
    Car\13\PlateNumber=12
    Car\14\Driver=Driver No. 13
    Car\14\PlateNumber=13
    Car\15\Driver=Driver No. 14
    Car\15\PlateNumber=14
    Car\16\Driver=Driver No. 15
    Car\16\PlateNumber=15
    Car\17\Driver=Driver No. 16
    Car\17\PlateNumber=16
    Car\18\Driver=Driver No. 17
    Car\18\PlateNumber=17
    Car\19\Driver=Driver No. 18
    Car\19\PlateNumber=18
    Car\2\Driver=Driver No. 1
    Car\2\PlateNumber=1
    Car\20\Driver=Driver No. 19
    Car\20\PlateNumber=19
    Car\3\Driver=Driver No. 2
    Car\3\PlateNumber=2
    Car\4\Driver=Driver No. 3
    Car\4\PlateNumber=3
    Car\5\Driver=Driver No. 4
    Car\5\PlateNumber=4
    Car\6\Driver=Driver No. 5
    Car\6\PlateNumber=5
    Car\7\Driver=Driver No. 6
    Car\7\PlateNumber=6
    Car\8\Driver=Driver No. 7
    Car\8\PlateNumber=7
    Car\9\Driver=Driver No. 8
    Car\9\PlateNumber=8
    Car\size=20
    How can I changed the sorting style of the ouput text , I want that all cars should be in order, not like above?
    ie Car\10\... should follow Car\9\...

    baray98

  5. #4
    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: structures to a file (read and write)

    What for? The order doesn't really matter, it's just a config file... If you want to change the order, prepend all single digit section names with "0". Of course you won't be able to use automatic section naming through array support.

  6. #5
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: structures to a file (read and write)

    So Ill take it as a NO ...can't do it

    and for your question of why......the answer is below

    I may need to edit this manually in the future and i dont want to be confused of how many cars do i have ?

  7. #6
    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: structures to a file (read and write)

    Quote Originally Posted by baray98 View Post
    I may need to edit this manually in the future and i dont want to be confused of how many cars do i have ?
    Qt Code:
    1. Car\size=20
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How can I Read Write Excel File
    By xingshaoyong in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2011, 21:16
  2. How to open a file in Read Write mode
    By merry in forum Qt Programming
    Replies: 13
    Last Post: 16th November 2007, 15:40
  3. Read \Write Excel File using Qt
    By xingshaoyong in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 23:07
  4. FSWriteFork in MAC OS to write data to a file.
    By vishal.chauhan in forum General Programming
    Replies: 5
    Last Post: 2nd July 2007, 07:48
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 15:12

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.