Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';无法打开2个文件*_C_File_Raspberry Pi_Printf - Fatal编程技术网

Can';无法打开2个文件*

Can';无法打开2个文件*,c,file,raspberry-pi,printf,C,File,Raspberry Pi,Printf,我在用C编写一个Raspberry PI开发程序,我发现了一个奇怪的bug 老实说,我对它的起源一无所知。到目前为止,程序非常简单 #include <bcm2835.h> #include <time.h> #include <sys/time.h> #include <stdint.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #i

我在用C编写一个Raspberry PI开发程序,我发现了一个奇怪的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!");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
文件*文件;
文件*file2;
文件*peak1;
文件*peak2;
file=fopen(“input0.txt”,“a+”);
file2=fopen(“input1.txt”,“a+”);
peak1=fopen(“peak1.txt”,“a+”);
peak2=fopen(“peak2.txt”,“a+”);
fprintf(文件“%s\n”、“HELLO!”);
fprintf(peak1,“%s\n”,“你好!”);
}
错误:-

当我运行程序并检查文件的输出时,只有
'input0.txt'
“HELLO!”
写在
'peak1.txt'
没有任何内容。
我可以写入前两个文件
file
file2
,但无法写入后两个文件
peak1
peak2

我试过写很多东西,但都没用。有什么问题吗


谢谢

你忘了在最后调用
fclose(FILE*)
。调用将确保正确处理文件描述符并刷新输出缓冲区,以便写入文件的数据将出现在磁盘上的文件中

发件人::


fclose()
函数应使流指向的流 将刷新并关闭关联的文件任何不成文的 流的缓冲数据应写入文件任何未读内容 应丢弃缓冲数据。无论呼叫是否成功, 流应与文件和由
setbuf()
setvbuf()
功能应与 流动如果相关缓冲区是自动分配的,则应 被解除分配


您需要在代码末尾调用
fclose(FILE*)


C
库函数int
fclose(文件*流)
关闭流。所有缓冲区都已刷新。

此外,您还包括了许多头文件,我不知道您的项目,但对于您发布的代码,您只需要
是的,我知道这一点。我打算让我的程序在以后做更多的事情,我知道我将来会需要这些标题。谢谢@MEric
如果我也不关闭前两个文件(文件/file2),为什么我可以写入它们?
——因为这并不意味着只有在
fclose()数据写入磁盘之后。。。但是
fclose()。读我回答的第一行。--明白吗?好的,我再补充一句。但是,如果不关闭前两个文件(file/file2),为什么还要写入它们?