Results 1 to 7 of 7

Thread: helping for writing and reading with only one file

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default helping for writing and reading with only one file

    Hi,
    Because of needing for saving some double, int and QStringList variables, my project works with 3 files that have written and read binary. these 3 files are for reading from them and setting some values from them and if there are some changing, I save (write) the changes in these files. I compiled my project statically and all of output is a standalone ".exe" and these 3 files beside of this ".exe".
    I want to put these 3 files inside the ".exe" but because of writing into them I can not put them into the resources folder.
    one idea is: writing all of my data (double, int and QStringList variables) only into the 1 file. please help me in this way how could I.
    and in the other hand, is it possible to put these 3 files into ".exe" of my project?

    my code:

    Qt Code:
    1. QFile g1f, g2f, of, namef // writing my 3 files
    2. g1f.setFileName("ga.bin");
    3. if(!g1f.open(QFile::WriteOnly))
    4. {
    5. qDebug()<<"can not open to write";
    6. }
    7. g1f.write((char*)g1, 500*sizeof(double));//1-writing binary my double variables into a file in name of "ga.bin"
    8. g1f.flush();
    9. g1f.close();
    10.  
    11. g2f.setFileName("g.bin");
    12. if(!g2f.open(QFile::WriteOnly))
    13. {
    14. qDebug()<<"can not open to write";
    15. }
    16. g2f.write((char*)g2, 500**sizeof(int));//2-writing binary my int variables into an another file in name of "g.bin"
    17. g2f.flush();
    18. g2f.close();
    19.  
    20. namef.setFileName("n.bin");//3--writing binary my QString variables into an another file in name of "n.bin"
    21. if(!namef.open(QFile::WriteOnly))
    22. {
    23. qDebug()<<"can not open to write";
    24. }
    25. QDataStream df(&namef);
    26. df << names;//names is an object of QStringList
    27. namef.flush();
    28. namef.close();
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QFile g1f1, g2f1, of1, namef1 //reading 3 files that has been written above
    2.  
    3. g1f1.setFileName("ga.bin");//1-reading binary the double variables
    4. if(!g1f1.open(QFile::ReadOnly))
    5. {
    6. qDebug()<<"can not open to read";
    7. }
    8. g1f1.read((char*)g1, 500**sizeof(double));
    9. g1f1.close();
    10.  
    11. g2f1.setFileName("g.bin");
    12. if(!g2f1.open(QFile::ReadOnly))
    13. {
    14. qDebug()<<"can not open to read";
    15. }
    16. g2f1.read((char*)g2, 500*sizeof(int));//2-reading binary the int variables
    17. g2f1.close();
    18.  
    19.  
    20. of1.setFileName("o.bin");
    21. if(!of1.open(QFile::ReadOnly))
    22. {
    23. qDebug()<<"can not open to read";
    24. }
    25. of1.read((char*)ofse, 500*sizeof(double));
    26. of1.close();
    27.  
    28. namef1.setFileName("n.bin");//3-reading binary the QString variables
    29. if(!namef1.open(QFile::ReadOnly))
    30. {
    31. qDebug()<<"can not open to read";
    32. }
    33. QDataStream fg(&namef1);
    34. fg >> names1;//names1 is an object of QStringList
    35. close();
    To copy to clipboard, switch view to plain text mode 

    first, can i put these 3 files (that i am reading from them and writing into them), into the ".exe" file and not beside of it? if not, then how can i have only one file instead of 3 files?

    thanks for any help

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: helping for writing and reading with only one file

    Windows will not allow you to change an EXE file while it is running, so you cannot add new text to the same file as your EXE. You have to use a separate file.

    I don't understand why you think you need 3 files. If the content is always 500 doubles and ints, then just write the doubles and then the ints to the same file. If you want to use QDataStream to read and write the strings, then use QDataStream to read and write the arrays of doubles and ints also.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: helping for writing and reading with only one file

    If the program needs a file for input on startup, you could do this:

    1) check if the file exists in the user's data location
    2) if not, extract from resource to the user's data location
    3) load file from the user's data location

    Writing can then always happen to that file.

    Cheers,
    _

  4. #4
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: helping for writing and reading with only one file

    Quote Originally Posted by d_stranz View Post
    If the content is always 500 doubles and ints, then just write the doubles and then the ints to the same file. If you want to use QDataStream to read and write the strings, then use QDataStream to read and write the arrays of doubles and ints also.
    Thanks for your answering. Yes the content is always 500 "doubles" and "ints" and 500 names in a "QStringList" (that length of each name is not constant and certain). please help me to write/read only one file in this way. thanks a lot

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: helping for writing and reading with only one file

    Simple make the write() and read() calls on the same file?

    Cheers,
    _

  6. #6
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: helping for writing and reading with only one file

    Quote Originally Posted by anda_skoa View Post
    If the program needs a file for input on startup, you could do this:

    1) check if the file exists in the user's data location
    2) if not, extract from resource to the user's data location
    3) load file from the user's data location

    Writing can then always happen to that file.

    Cheers,
    _
    Really thanks, nice way. but i have some questions:
    - how could extract from resource? (by this i could read/write and files are not beside of the ".exe")
    - is it possible to load from the user's data location to resource?

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: helping for writing and reading with only one file

    Quote Originally Posted by Alex22 View Post
    - how could extract from resource? (by this i could read/write and files are not beside of the ".exe")
    QFIle::copy() should work, otherwise readAll() on a QFile that has the resource path and writing that into the QFile at the data location.

    Quote Originally Posted by Alex22 View Post
    - is it possible to load from the user's data location to resource?
    Not sure what you mean.
    QFile based loading is transparent on whether the path is a resource or a local file.

    With the described "extract" approach you would never read from the resource directly, always from the local file.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    Alex22 (5th January 2016)

Similar Threads

  1. qt binary file writing and reading
    By seniorc in forum Newbie
    Replies: 9
    Last Post: 17th December 2013, 23:03
  2. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24
  3. Reading and writing bytes in file
    By DiamonDogX in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 20:25
  4. reading/writing to file
    By QiT in forum Newbie
    Replies: 2
    Last Post: 11th August 2006, 17:21
  5. Replies: 6
    Last Post: 27th February 2006, 12:47

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.