Important: Please read the Qt Code of Conduct -

What would be the best way to read and write multiple values to/from file for none tabular data

  • Hi,

    I have been successfully reading and writing to a file using model views (QStandardItemModel/TableView) but this was mostly tabular data. What I still don't know is how nor what would be the best way to read and write multiple values to a file. In other words what is the best way to save multiple values to a single file and still be able to read each value separately? I know I could use a separate file for each value but I would end up reading and writing to a lot of files.

    Again, what would be the most common method to read and write multiple values to/from file? Using multiple file? One for each value?

    I'm not talking about data that will be displayed together, these are values that will be used and could change throughout the program but need to be saved so the users can change them if they wanted to .

    Something like...

    @rate1 = 56.3 hrs.
    rate2 = 14.6 hrs.
    rate3 = 45.4 hrs.
    rate50 = 23.4 hrs@

    As you can see saving 50 different file would be a tedious job.


  • Moderators


    These look like config values. Would QSettings suit your needs? It lets you set/get values independently.

  • I alway thought that QSettings was more to save things like wondow geometry etc. things that don't change so often, but I dont know, is this what you would use?

  • Moderators

    How often is "often"? What are these values used for?

  • Thank you for your reply.

    Some calculations will be done based on these values and now that I'm thinking these values are going to be shared in a local network because all calculations made by the users need to be made based on the same values so, I dont think QSettings is an options. Thanks

  • You can write the values in one XML File. One Tag for each Value. To access the Data use XQuery or XMLReader.

  • Ok, I will look into XQuery or XMLReader. Thanks a lot

  • Why not use a database server? Qt has all kinds of database plugins. The database does not have to be local. Usually database servers are exactly for this purpose.

    But if you want to save this at speeds like 20 billion times per second the you need some real hardcore C programming and write the data in binary.

    make an array of 50 doubles.
    You can also read individual elements. You just need to calculate the offset. For example on my system the size of a double is 8 bytes. if you want to read the 33rd rate you just seek to sizeof(double) * (33 -1) /*if you start from rate1 not from rate0 */

    This saves the whole array in one shot.

    @ double rate[50];
    // put some sample data...
    for( int i=0;i<50;i++)
    rate[i]= (double)i+ (double)i/3;

    // ... and save it
    QFile f( "rates.dat");  QIODevice::WriteOnly);
    f.write( (char*) &rate, 50*sizeof(double));


    But the downside if you are using this in financial institution where there are a lot of java, .net tools and excell macros, they won't be able to read binary data. Also you need to take care of endianness when working with mainframes and stuff.

  • I dont have direct access to a server to install a database but I like the array method, I will give this a try . Thanks a lot

  • Moderators

    SQLite can be used without server-side installations -- the database is a single local file.

    With the array method, be aware that you'll have to read/write all values simultaneously instead of separately (which you indicated in your original post), unless you include code that counts bytes.

  • I ended up using a QHash and seems to be working fine. Thank you all very much for your suggestions.

    @ QHash<QString, QVariant> myList;
    myList.insert("key1", 23);
    myList.insert("key2", 3.4);
    myList.insert("key3", 22.7);

     QFile fileName("file.txt");
     if (
         QDataStream writeToFile&#40;&fileName&#41;;
         writeToFile << myList;


    @ QHash<QString, QVariant> myList;
    QFile fileName("file.txt");
    if (
    QDataStream readFromFile(&fileName);

            readFromFile >> myList;

    Then I can retrive/show values individually like so...

Log in to reply