Results 1 to 5 of 5

Thread: Best way to write to a file

  1. #1

    Default Best way to write to a file

    I'm building a save file, I have various functions that need to write to the same file one after the other.

    Right now I'm using this code to open the file and write to it.

    Qt Code:
    1. bool someFunction(QString fileName)
    2. {
    3. QFile file(fileName);
    4. if(!file.open(QIODevice::WriteOnly)) //sometimes opened in Append mode
    5. return 0;
    6.  
    7. QDataStream out(&file);
    8. out.setVersion(QDataStream::Qt_4_7);
    9. out << //here goes the stuff
    10. return 1;
    11. }
    To copy to clipboard, switch view to plain text mode 

    So I open the file every time, would it be more efficient to open it just one time and then pass the various functions a pointer, like this ?
    Qt Code:
    1. bool someFunction(QFile *file)
    To copy to clipboard, switch view to plain text mode 

    Or should I pass the QDataStream altogether?
    Qt Code:
    1. bool someFunction(QDataStream out)
    To copy to clipboard, switch view to plain text mode 

    BTW does the file need to be closed at the end of the function?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Best way to write to a file

    The question is, how do these functions write to the file?
    Do you append everything, or overwrite the file, or something else?

    Did you consider using QSettings?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best way to write to a file

    If you write to the file in several steps, then you should only open it once. You should simply open the file and create the QDataStream, then pass a pointer or a reference to the QDataStream around (be careful, your second suggestion passes the QDataStream by value, which you do not want to do, and probably cannot do due to a non-public copy constructor).

    QFile automatically closes the file in its destructor if it has not been done yet, so do not worry about it. Just make sure that the QDataStream, then the QFile get destroyed after you are done writing.

  4. #4

    Default Re: Best way to write to a file

    I have one function that opens the file to overwrite everything, then other functions append infos.
    Maybe I should open it like this the first time
    Qt Code:
    1. file.open(QIODevice::Truncate)
    To copy to clipboard, switch view to plain text mode 
    If there is no file to truncate, does it get created?

    Thank you, I'll pass a reference to the QDataStream, like this
    Qt Code:
    1. bool someFunction(QDataStream &out)
    To copy to clipboard, switch view to plain text mode 

    I'm saving with a .txt extension, so I can have a look when I need to debug, does it make a difference?

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best way to write to a file

    If there is no file to truncate, does it get created?
    Yes, as long as you do not forget QIODevice::WriteOnly. From the docs:
    The mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or QIODevice::ReadWrite. It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered.
    I'm saving with a .txt extension, so I can have a look when I need to debug, does it make a difference?
    No, the contents of the file will not be affected. However QDataStream will produce binary data; I would use a hex editor rather than a text editor.

Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2010, 05:15
  2. about qt debugger and file write
    By guchangyuan in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2009, 02:58
  3. How to write data into a file
    By grsandeep85 in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2009, 08:51
  4. About File write operation
    By raghvendramisra in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2008, 11:41
  5. Uic cannot write output file
    By jobrandt in forum Qt Tools
    Replies: 4
    Last Post: 25th May 2007, 07:55

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.