creating a logfile

Started by
4 comments, last by Chris Thomas 22 years, 3 months ago
hi, I''m trying to create a logfile in my game by simply using fprintf and outputing strings to a file. The main reason i''m doing this is to debug some parts of my network code to make sure that what i want to be sent is being sent. The problem I have is that if the program exits because of an error the logfile is left blank. It works fine when the program exits normally. Any help would be appreciated.
Advertisement
Add calls to fflush() to ... flush the buffer.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Even better:
setvbuf(fp, NULL, _IONBF, 0); 

What does it do? It sets the specified filestream to be unbuffered, meaning its data is immediately written to disk.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
If it''s completely blank, you''re probably not even calling fclose(). Everything will be flushed when you close the file. However, as was already suggested, you should also use fflush() occasionally in case you''re program crashes (in which case fclose() is definitely never called, and you''re logfile is blank).
But if you use stvbuf, you don''t have to call fflush and you can let the program crash all it wants to. The data will be there in the log file.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Thanks for the info.
I will try this out right away.
These forums rock..

This topic is closed to new replies.

Advertisement