Results 1 to 2 of 2

Thread: Read a file containing key-value pair

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    675
    Thanks
    55
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read a file containing key-value pair

    Hi Community,
    I need to read and parse a file containing key-value pairs (only 1 line):

    -t int_value -d string_value -t int_value -d string_value
    The number and order of -d or -t keys is arbitrary

    example:
    -t 12 -t 49 -d "option_x" -d "option_y"
    I don't know the number of pairs -t value or -d value.

    Any suggestion on the approach I can use?

    Thank you


    Added after 10 minutes:


    I tried this approach
    Qt Code:
    1. QFile parametersFile(parametersFilePath);
    2. if (parametersFile.open(QIODevice::ReadOnly | QIODevice::Text))
    3. {
    4. QTextStream stream(&parametersFile);
    5. QString line = stream.readLine();
    6. if (!line.isEmpty())
    7. {
    8. QStringList fields = line.split(' ');
    9. for (int i = 0; i < fields.count();)
    10. {
    11. if (fields[i] == '-t')
    12. {
    13. map.insert("EDIT_PARAMETER", fields[i+1]);
    14. }
    15. else if (fields[i] == '-d')
    16. {
    17. map.insert("COMBO_PARAMETER", fields[i+1]);
    18. }
    19.  
    20. i+=2;
    21. }
    22. }
    23.  
    24. return;
    25. }
    To copy to clipboard, switch view to plain text mode 

    But it's not working as expected
    Last edited by franco.amato; 14th February 2022 at 21:08.
    Franco Amato

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    487
    Thanks
    11
    Thanked 71 Times in 69 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Read a file containing key-value pair

    Hi, what happens when you use your code? Normally a debugger can help you with stuff like this.

    I think to avoid strange effects you should use QString::SkipEmptyParts in line.split().

    Ginsengelf

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

    franco.amato (15th February 2022)

Similar Threads

  1. Replies: 6
    Last Post: 24th August 2015, 06:25
  2. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  3. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 15:42
  4. Replies: 1
    Last Post: 20th June 2008, 18:43
  5. Replies: 2
    Last Post: 8th November 2007, 20:15

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.