简体   繁体   English

使用许多对fprintf的调用; 什么时候应该使用fflush?

[英]Using many calls to fprintf; when should I use fflush?

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). 有很多对fprintf()的调用(大致线性依赖于要转换的文件的大小)。 When I started, there were no calls to fflush(). 当我开始时,没有调用fflush()。 On small files (<10 Kb) there was no problem whatsoever. 在小文件(<10 Kb)上,没有任何问题。 On a larger file, (>40 Kb) the whole thing crashed when the call to fclose() was reached. 在更大的文件上(> 40 Kb),在调用fclose()时整个崩溃。

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). 因此,我以为可能是让缓冲区填满了太多或什么东西,所以在大约512次对fprintf的调用(每个调用输出8到10个字符)之后,我对fflush()进行了调用。 The program still crashes on the call to fclose(). 调用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. *因为我实际上没有计算对fprintf的调用,而我使用的是程序中已经存在的另一个计数,所以该数字可能小于512。

This brings me to my question. 这使我想到了我的问题。 When should fflush() be called? 何时应调用fflush()? Should it be called after a certain amount of data has been fprintf'd? 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 顺便说一句,如果相关的话,我在Windows 7(64位)上,并且已经以“ a +”模式打开了输出文件

It is completely legitimate to call fprintf as many times as one needs without a single call to fflush . 多次调用fprintf是完全合法的,而无需一次调用fflush Crashes are caused by something else in your program, most likely some invalid memory access, and adding fflush calls will not fix them. 崩溃是由程序中的其他原因引起的,很可能是某些无效的内存访问,而添加fflush调用将无法修复它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM