Why isn't the file saved during execution in C?

42 Views Asked by At

The source code of program_1 is as follows.

/* program_1.c */

#include <stdio.h>

int main(void)
{
FILE *ofp;

ofp = fopen("myfile", "w");

fprintf(ofp, "test");

getchar();

fclose(ofp);

getchar();

return 0;

}

The terminal running one program_1 and another terminal for viewing the contents of myfile are first displayed.

  1. If I check the contents of myfile while running program_1, string "test" is not printed.

  2. However, if I input the enter key once while running program_1 and check the contents of myfile, I can see that the "test" string has been printed.

question : Why is this happening?

0

There are 0 best solutions below