Results 1 to 9 of 9

Thread: read ini file content and save into array

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question read ini file content and save into array

    Hi everyone,

    In my program, i need to read an ini file to an array. here is my ini file:
    Qt Code:
    1. [Config]
    2. Number1=156
    3. Number2=4
    4.  
    5. [StageNum]
    6. 1\cy1\7
    7. 1\cy1\2
    8. 1\cy1\5
    9. 1\cy1\2
    10. 1\cy1\3
    11. 1\cy1\0
    To copy to clipboard, switch view to plain text mode 

    in "StageNum", the first digit indicates all number in the same group (that's what i understand). "cy1" represents the name of array. the third digit is the value i want to read and save into an array.

    i can read the single value in "config" successfully in my code, but i could not read "StageNum" into an array at all. Can anyone point out for me:

    1) if the format of array setting is correct in ini file? if not, what is the right format?
    2) if the format of array is correct in ini file, where does my code go wrong?

    here is part of my code:
    Qt Code:
    1. int arySize = m4Settings->beginReadArray("StageNum");
    2. for (int vL = 0; vL < arySize; vL ++)
    3. {
    4. m4Settings->setArrayIndex(vL);
    5. stageNo[vL] = m4Settings->value("cy1","0").toInt();
    6. }
    7. m4Settings->endArray();
    To copy to clipboard, switch view to plain text mode 
    when i step through the code, the "arySize" read 0 instead of 6.

    Thanks in advance.

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: read ini file content and save into array

    the documentation for QSettings shows forward slash '/', whereas you are using backslash '\'.

    Documentation mentions:

    QSettings always treats backslash as a special character and provides no API for reading or writing such entries.
    The documentation shows an example of the correct format:
    http://doc.qt.nokia.com/stable/qsett...eginWriteArray
    Last edited by schnitzel; 11th March 2011 at 01:40.

  3. #3
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: read ini file content and save into array

    Hi schnitzel,

    Thanks for your reply.
    the file i am going to read is a .ini file, in the QSettings doc, it says:
    Although backslash is a special character in INI files, most Windows applications don't escape backslashes (\) in file paths:
    when i define my m4Settings, i was using:
    Qt Code:
    1. m4Settings = new QSettings(QString(QApplication::applicationDirPath()).append("/m4ini"), QSettings::IniFormat);
    To copy to clipboard, switch view to plain text mode 

    EDIT:

    I have changed my [StageNum] content to use "/", but still, i got 0 for "arySize".
    Last edited by cooper; 11th March 2011 at 01:59.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: read ini file content and save into array

    Your syntax is all wrong. The first field in each entry is the index, the second the name of the value, and (you are completely missing this) the value itself comes after an equal sign. You are also missing the size attribute.

    This defines a 6-element array called StageNum, where each element has a value called "cy1" (test.ini):
    Qt Code:
    1. [Config]
    2. Number1=156
    3. Number2=4
    4.  
    5. [StageNum]
    6. size = 6
    7. 0/cy1 = 7
    8. 1/cy1 = 2
    9. 2/cy1 = 5
    10. 3/cy1 = 2
    11. 4/cy1 = 3
    12. 5/cy1 = 0
    To copy to clipboard, switch view to plain text mode 
    and this will read it:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSettings>
    3. #include <QDebug>
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication app(argc, argv);
    9.  
    10. QSettings m4Settings("test.ini", QSettings::IniFormat);
    11.  
    12. int arySize = m4Settings.beginReadArray("StageNum");
    13. qDebug() << "arySize =" << arySize;
    14. for (int vL = 0; vL < arySize; vL ++)
    15. {
    16. m4Settings.setArrayIndex(vL);
    17. qDebug()
    18. << "index =" << vL
    19. << "cy1 =" << m4Settings.value("cy1","0").toInt();
    20. }
    21. m4Settings.endArray();
    22.  
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    cooper (13th March 2011)

  6. #5
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: read ini file content and save into array

    hi ChrisW67,

    Thanks for your help. It's work.
    I really appreciate that

    EDIT:

    but one more problem arises:
    if my .ini file's content is
    Qt Code:
    1. [StageNum]
    2. size = 6
    3.  
    4. 0/cy1 = 7
    5. 1/cy1 = 2
    6. 2/cy1 = 5
    7. 3/cy1 = 2
    8. 4/cy1 = 3
    9. 5/cy1 = 1
    To copy to clipboard, switch view to plain text mode 
    and my code is
    Qt Code:
    1. int arySize = m4Settings->beginReadArray("StageNum");
    2. for (int vL = 0; vL < arySize; vL ++)
    3. {
    4. m4Settings->setArrayIndex(vL);
    5. cyNum[vL] = m4Settings->value("cy1", 0).toInt();
    6. }
    7. m4Settings->endArray();
    To copy to clipboard, switch view to plain text mode 
    the value of "cyNum" is:
    Qt Code:
    1. cyNum[0] = 2
    2. cyNum[1] = 5
    3. cyNum[2] = 2
    4. cyNum[3] = 3
    5. cyNum[4] = 1
    6. cyNum[5] = 0
    To copy to clipboard, switch view to plain text mode 

    I have step through the code, when vL = 0, cyNum[0] = 2...
    I don't see where my code gets wrong. but why it read the value from second line.

    then i tried:
    Qt Code:
    1. int arySize = m4Settings->beginReadArray("StageNum");
    2. for (int vL = 0; vL < arySize; vL ++)
    3. {
    4. m4Settings->setArrayIndex(vL - 1); // made a little change here
    5. cyNum[vL] = m4Settings->value("cy1", 0).toInt();
    6. }
    7. m4Settings->endArray();
    To copy to clipboard, switch view to plain text mode 
    i got:
    Qt Code:
    1. cyNum[0] = 2
    2. cyNum[1] = 2
    3. cyNum[2] = 5
    4. cyNum[3] = 2
    5. cyNum[4] = 3
    6. cyNum[5] = 1
    To copy to clipboard, switch view to plain text mode 

    can anyone where i did it wrong, please?
    Last edited by cooper; 14th March 2011 at 01:41.

  7. #6
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: read ini file content and save into array

    I end up with changing my ini file and code as:
    .ini
    Qt Code:
    1. [StageNum]
    2. size = 6
    3. 1/cy1 = 7
    4. 2/cy1 = 2
    5. 3/cy1 = 5
    6. 4/cy1 = 2
    7. 5/cy1 = 3
    8. 6/cy1 = 1
    To copy to clipboard, switch view to plain text mode 
    code:
    Qt Code:
    1. int arySize = m4Settings->beginReadArray("StageNum");
    2. for (int vL = 1; vL <= arySize; vL ++) // made a little change here
    3. {
    4. m4Settings->setArrayIndex(vL - 1); // made a little change here
    5. cyNum[vL] = m4Settings->value("cy1", 0).toInt();
    6. }
    7. m4Settings->endArray();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. cyNum[0] = 9076386
    2. cyNum[1] = 7
    3. cyNum[2] = 2
    4. cyNum[3] = 5
    5. cyNum[4] = 2
    6. cyNum[5] = 3
    7. cyNum[6] = 1
    To copy to clipboard, switch view to plain text mode 

    but i just cheat it. it is not what i really want. any good idea?

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: read ini file content and save into array

    You are reading index vL-1 and putting it into the array at index vL. What you see in the array at index 0 is whatever random rubbish was in the memory the array was allocated.

    There's nothing I can see wrong with a round-trip through QSettings:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSettings>
    3. #include <QDebug>
    4.  
    5. int data[] = { 3, 1, 4, 1, 5, 9 };
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QCoreApplication app(argc, argv);
    10.  
    11. QSettings m4Settings("test.ini", QSettings::IniFormat);
    12.  
    13. // Write a test file
    14. m4Settings.beginWriteArray("StageNum");
    15. for (unsigned int i = 0; i < sizeof(data)/sizeof(int); ++i) {
    16. m4Settings.setArrayIndex(i);
    17. m4Settings.setValue("cy1", data[i]);
    18. }
    19. m4Settings.endArray();
    20.  
    21. // read it back
    22. int arySize = m4Settings.beginReadArray("StageNum");
    23. qDebug() << "arySize =" << arySize;
    24. for (int vL = 0; vL < arySize; vL ++)
    25. {
    26. m4Settings.setArrayIndex(vL);
    27. qDebug()
    28. << "index =" << vL
    29. << "cy1 =" << m4Settings.value("cy1", "0").toInt();
    30. }
    31. m4Settings.endArray();
    32.  
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    and this is what the file QSettings wrote looks like:
    Qt Code:
    1. [StageNum]
    2. 1\cy1=3
    3. 2\cy1=1
    4. 3\cy1=4
    5. 4\cy1=1
    6. 5\cy1=5
    7. 6\cy1=9
    8. size=6
    To copy to clipboard, switch view to plain text mode 
    Arrays are zero-based in C/C++, and everyone expects that, but it seems QSettings wants to behave differently



    What are you ultimately trying to achieve with all of this? If you simply want to initialise an array there are easier ways to go about it.
    Last edited by ChrisW67; 14th March 2011 at 04:12.

  9. The following user says thank you to ChrisW67 for this useful post:

    cooper (14th March 2011)

  10. #8
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: read ini file content and save into array

    If you simply want to initialise an array there are easier ways to go about it.
    Yes, I just want to initialise an array by read from an ini file.
    Do you have any good suggestion please?
    I am still learning Qt and C++.
    Thanks for your help.

  11. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: read ini file content and save into array

    If you want to initialise an array or arrays (that can vary) from an INI file, or store and reload a variable array, then QSettings is as good a way as any (once you get past the quirks). You could just create a flat text file in your own format, e.g. one line per record with fields of your choosing, and read/write it with QTextStream.

    If you just want to initialise an array with fixed values you can do what I do in the example above.

  12. The following user says thank you to ChrisW67 for this useful post:

    cooper (15th March 2011)

Similar Threads

  1. read a .txt file and store it in a double array
    By fatecasino in forum Newbie
    Replies: 5
    Last Post: 3rd December 2010, 20:13
  2. QDomDocument can't read the file content
    By baluk in forum Newbie
    Replies: 21
    Last Post: 24th September 2010, 13:43
  3. read from file into array
    By obad in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2010, 08:26
  4. Save content of widget to pixmap
    By vql in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2008, 22:26
  5. how to copy QString content into array
    By eric in forum Qt Programming
    Replies: 12
    Last Post: 14th November 2007, 23:13

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.