简体   繁体   中英

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:

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:

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); directly after the fprintf call, the program successfully writes :

This is testing for 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 ?

  • What are the factors that prevent fprintf() from working ? (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 ?

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.

What are the factors that prevent fprintf() from working ? (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() . 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).

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.

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(file, "This is testing for fprintf...\n");
fflush(file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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