我当时正在为Raspberry PI开发编写一个C语言的程序,我已经得到了这个奇怪的bug。
我真的不知道它的起源。到目前为止,这个程序非常简单。
#include <bcm2835.h>
#include <time.h>
#include <sys/time.h>
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
int main(int argc, char *argv[])
{
FILE *file;
FILE *file2;
FILE *peak1;
FILE *peak2;
file = fopen("input0.txt", "a+");
file2 = fopen("input1.txt", "a+");
peak1=fopen("peak1.txt", "a+");
peak2=fopen("peak2.txt", "a+");
fprintf(file, "%s\n", "HELLO!");
fprintf(peak1, "%s\n", "HELLO!");
}
Bug:-
当我运行程序并检查文件的输出时,只有'input0.txt'
编写了"HELLO!"
,因为'peak1.txt'
没有任何东西。
我可以写入前两个文件file
和file2
,但不能写入第二个文件peak1
和peak2
。
我试着写了很多东西,但都没有用。有什么问题吗?
谢谢!
发布于 2013-07-27 08:54:33
最后你忘了给fclose(FILE *)
打电话了。调用int fclose(FILE *fp);
将确保正确地处理文件描述符,并刷新输出缓冲区,从而将写入文件的数据显示在磁盘上的文件中。
(小溪);
fclose()
函数将导致按流指向的流被刷新并关闭相关文件。流的任何未写入的缓冲数据应写入文件;任何未读取的缓冲数据将被丢弃。无论调用是否成功,流将与文件断开关联,由setbuf()
或setvbuf()
函数设置的任何缓冲区都将与流断开关联。如果自动分配了关联缓冲区,则应将其解除分配。
发布于 2013-07-27 08:56:11
您需要在代码的末尾调用fclose(FILE *)
。
C
库函数int fclose(FILE *stream)
关闭流。所有的缓冲器都被冲了。
https://stackoverflow.com/questions/17900250
复制相似问题