简体   繁体   English

C程序未写入文本文件

[英]C program not writing to text file

So here is the problem: 所以这是问题所在:

I want to write some text to a text file , but I got across some weird situations: 我想在文本文件中写一些文本,但是遇到了一些奇怪的情况:

SAMPLE 1: 范例1:

int main()
{
    FILE *file = fopen("structures.txt", "a+"); // open the file for reading & writing
    int choice = 0;

    if(file == NULL)
        puts("Unable to open text file");
    else { 
        do {
            scanf("%d", &choice);
            fprintf(file, "This is testing for fprintf...\n");
        }while(choice > 0);
    }
    fclose(file);

    return 0;
}

In this version of the program, nothing ever gets written to the text file & I can't understand why. 在此版本的程序中,没有任何内容写入文本文件,我也不明白为什么。 But the strangest thing for me is next: 但是对我来说最奇怪的是:

SAMPLE 2: 范例2:

int main()
{
    FILE *file = fopen("structures.txt", "a+"); // open the file for reading & writing
    int choice = 0;

    if(file == NULL)
        puts("Unable to open text file");
    else { 
        do {
            scanf("%d", &choice);
            fprintf(file, "This is testing for fprintf...\n");
            fclose(file); // Added this line, the writing works
        }while(choice > 0);
    }
    fclose(file);

    return 0;
}

After adding fclose(file); 添加fclose(file); directly after the fprintf call, the program successfully writes : 直接在fprintf调用之后,程序成功写入:

This is testing for fprintf... 这是测试fprintf ...

to the text file. 到文本文件。

My questions are: 我的问题是:

  • Does that mean that I have to open my text file whenever I want to write some text to it & close it directly afterwards ? 这是否意味着每当我想向其中写入文本并随后直接将其关闭时,都必须打开文本文件?

  • Does fclose() has anything to do with the writing process ? fclose()编写过程有关系吗?

  • What are the factors that prevent fprintf() from working ? 是什么因素导致fprintf()无法正常工作? (1st sample) (第一个样本)

  • How can I open & close the text file just ONCE, at the start of the program & at the end of it (respectively) guaranteeing at the same time that my program will work flawlessly ? 在程序开始时和结束时,如何才能一次打开和关闭文本文件(分别),以确保我的程序可以正常运行?

Does that mean that I have to open my text file whenever I want to write some text to it & close it directly afterwards ? 这是否意味着每当我想向其中写入文本并随后直接将其关闭时,都必须打开文本文件?

No, but it may mean you need to close the file before the contents you've written will actually be flushed and written out to the file. 不,但是这可能意味着您需要先关闭文件,然后才能将写入的内容实际清空并写出到文件中。

Does fclose() has anything to do with the writing process ? fclose()与编写过程有关系吗?

Most file streams are buffered. 大多数文件流都被缓冲。 Meaning that each write goes to memory. 意思是每次写到内存。 It is not written to disk until the buffer is full or you call close. 在缓冲区已满或调用close之前,它不会写入磁盘。

What are the factors that prevent fprintf() from working ? 是什么因素导致fprintf()无法正常工作? (1st sample) (第一个样本)

Anything you get significantly wrong. 您发现任何严重错误。

How can I open & close the text file just ONCE, at the start of the program & at the end of it (respectively) guaranteeing at the same time that my program will work flawlessly ? 在程序开始时和结束时,如何才能一次打开和关闭文本文件(分别),以确保我的程序可以正常运行?

You could call something like fflush() . 您可以调用fflush()类的东西。 But are you sure you tried this and the file contained nothing even after you finally closed the file at the end? 但是,您确定尝试过此方法,即使最后最后关闭了文件,文件也没有任何内容吗?

I solved the problem using int fflush (FILE *stream) (which @Jonathan hinted at). 我使用int fflush (FILE *stream) (@Jonathan暗示了)解决了这个问题。

Flushing output on a buffered stream means transmitting all accumulated characters to the file. 在缓冲流上刷新输出意味着将所有累积的字符传输到文件。 There are many circumstances when buffered output on a stream is flushed automatically: 在许多情况下,流中的缓冲输出会自动刷新:

When you try to do output and the output buffer is full. 当您尝试执行输出并且输出缓冲区已满时。

When the stream is closed. 流关闭时。

When the program terminates by calling exit. 当程序通过调用exit终止时。

When a newline is written, if the stream is line buffered. 写入换行符时,如果流是行缓冲的。

Whenever an input operation on any stream actually reads data from its file. 每当任何流上的输入操作实际从其文件中读取数据时。

So basically, I just had to call fflush() after the call to fprintf() : 所以基本上,我只需要在调用fprintf()之后再调用fflush() fprintf()

fprintf(file, "This is testing for fprintf...\n");
fflush(file);

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

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