2

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

4

1 回答 1

6

fprintf无需一次调用即可根据需要多次调用是完全合法的fflush。崩溃是由程序中的其他原因引起的,很可能是一些无效的内存访问,并且添加fflush调用不会修复它们。

于 2014-07-31T19:12:42.943 回答