无法打开 2 个文件 *

标签 c file raspberry-pi printf

我正在用 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'什么都没有。
我可以写入前两个文件 filefile2,但无法写入后两个文件 peak1peak2

我试过写很多东西但无济于事。可能是什么问题呢?

谢谢!

最佳答案

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

发件人:IEEE Std 1003.1, 2004 Edition :

int fclose(FILE *stream);
The fclose() 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 the setbuf() or setvbuf() 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/

相关文章:

c++ - 自动填写表格并运行程序

c++ - 涉及字符串输入和字符串比较的scanf问题

c - fputc 无法处理大文件?段错误 11

php - 如何将一个目录移动到另一个目录?

Windows BAT : test if a specific file is empty

C Web 服务器 - 打印传入数据?

python - 如果检测到物体则播放声音

bash - rc.local 文件不工作树莓派

c - "__aeabi_ldivmod"编译内核模块时未定义

c - 为什么我的 fflush 不起作用?跳过循环中询问字符串