我正在用 C 编写一个用于 Raspberry PI 开发的程序,我遇到了这个奇怪的错误。
老实说,我对它的起源一无所知。到目前为止,该程序非常简单。
#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!");
}
错误:-
当我运行程序并检查文件的输出时,只有 'input0.txt'
将 "HELLO!"
写为 'peak1。 txt'
什么都没有。
我可以写入前两个文件 file
和 file2
,但无法写入后两个文件 peak1
和 peak2
。
我试过写很多东西但无济于事。可能是什么问题呢?
谢谢!
最佳答案
你忘了在最后调用 fclose(FILE *)
。来电 int fclose(FILE *fp);
将确保正确处理文件描述符并刷新输出缓冲区,以便写入文件的数据将出现在磁盘上的文件中。
发件人:IEEE Std 1003.1, 2004 Edition :
int fclose(FILE *stream);
Thefclose()
function shall cause the stream pointed to by stream to be flushed and the associated file to be closed. Any unwritten buffered data for the stream shall be written to the file; any unread buffered data shall be discarded. Whether or not the call succeeds, the stream shall be disassociated from the file and any buffer set by thesetbuf()
orsetvbuf()
function shall be disassociated from the stream. If the associated buffer was automatically allocated, it shall be deallocated.
关于无法打开 2 个文件 *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17900250/