Skip to main content
GameDev.net gamedev.net
🔒 Locked

forcing a flush SOLVED

Started by zedzeek Jan 31, 2006 at 3:33 PM 1 replies 750+ views
Original Post
zedzeek
zedzeek
im trying to force a flush after each line in my debug file, but its not working ofstream the_log_file; // ios::out std::stringstream stream; stream << "..[" << g_OS.timer.return_totalticks_NOW() << "] " << msg << endl; the_log_file << stream.str(); the_log_file.flush(); and the end of the file output is blah blah ..[4240] loading material template HUD_icons.xml ..[4254] loading material template HUD_icons_hilights.xml ..[4254] loadin the crash occurs about 5 seconds after '..[4254] loadin' so somehow its not flushing after each line what have i missed? ta zed [Edited by - zedzeek on January 31, 2006 4:18:18 PM]
Oluseyi
Oluseyi
[Edit: You posted while I was composing this reply and verifying the information! I'll leave it in case someone finds it useful.]


Have you run it in the debugger to get a handle on real-time data characteristics?

endl inserts a newline into a stream and then a flush, so your particular use of std::ofstream::flush is completely redundant. In any case, the key is that C++ stream I/O is buffered by default, and merely flushing the stream is not enough to ensure you don't lose any data to an application crash. You actually have to switch buffering off.

There is no way to switch buffering on C++ file stream objects off (simply because there is no exposed method to grant access to the FILE pointer). If you absolutely must have unbuffered (immediate) commits (generally not a good idea, as it's very inefficient), you will have to use C I/O - FILE pointers, calling setvbuf(fp, buf, _IONBF, n) right after opening the file.

Given the above, I think your error is elsewhere. Run in the debugger, and perform pre- and post-crash analysis. Good luck, and share whatever you learn!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.