I usually use two methods to write to files, either with Qt’s QFile or STL’s fstream.
I have a long-running (several minutes) simulation which logs data to a file. Performance-wise and design-wise, is it a good idea to:
- Keep it open the whole time
- Close and open on every write
- Somewhere in-between (1) and (2)
Several question on here address this issue (for Perl, for fopen), but I didn’t see any discussion of QFile and fstream. The previous answers suggest to keep it open (option 1). Is this still the cast for QFile and fstream?
Performance wise, it would definitely be better to keep it open for the life of the application, just because less work opening and closing files means less time spent doing things that don’t move the application closer to completion, which will slow the program down. As for design, just make sure to close the file before the application terminates.
QFile and fstream probably use fopen, fwrite etc under the hood (although it is of course implementation dependent). So I would bet that anything applying to
FILE*s would apply to QFiles and fstreams.