Skip to content

Instantly share code, notes, and snippets.

@vsr
Created November 25, 2013 16:20
Show Gist options
  • Save vsr/7643984 to your computer and use it in GitHub Desktop.
Save vsr/7643984 to your computer and use it in GitHub Desktop.
The program opens a file, prints in a loop and then closes it. Moving the file to another location, (`mv file.txt file1.txt`) during the loop does not cause any exception or error. At the end of the program, `file1.txt` has the proper output.
#include <stdio.h>
main(){
printf("Hello there!\n");
FILE *f = fopen("/tmp/file.txt", "w");
if(f == NULL){
printf("Error opening file!\n");
return 1;
}
int i = 0;
for(i=0; i<20;i++){
fprintf(f, "%d\n", i);
printf("Printing %d\n", i);
sleep(1);
}
fclose(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment