Question

I have a simple little program that converts one filetype to another. There are quite a few calls to fprintf() (roughly linearly dependent to the size of the file to convert). When I started, there were no calls to fflush(). On small files (<10 Kb) there was no problem whatsoever. On a larger file, (>40 Kb) the whole thing crashed when the call to fclose() was reached.

So, I thought maybe I was letting the buffer fill up too much or something, so I put in calls to fflush() after roughly* 512 calls to fprintf (where each call prints out between 8 and 10 characters). The program still crashes on the call to fclose().

*Because I'm not actually counting the calls to fprintf and I'm using another count that's already in the program, it is possible for this number to be less than 512.

This brings me to my question. When should fflush() be called? Should it be called after a certain amount of data has been fprintf'd? Or is there something I'm missing?

Thanks

By the way, in case it's pertinent, I'm on Windows 7 (64bit) and I've fopen'd the output file in "a+" mode

Was it helpful?

Solution

It is completely legitimate to call fprintf as many times as one needs without a single call to fflush. Crashes are caused by something else in your program, most likely some invalid memory access, and adding fflush calls will not fix them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top