Hello. I have this simple program that open a file and read lines from it and print them to screen.
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
char line[4096];
FILE *fp = fopen("/root/Desktop/testfile","r");
if( fp == NULL ) {
exit(1);
}
while(fgets( line, sizeof(line), fp ) != NULL ) {
printf("%s",line);
}
if( fp != NULL ) {
fclose(fp);
}
return 0;
}
There is nothing wrong withthe program. It works very well, but i have descovered that if i follow the next scenario :
- i start the program
- while the program is running i delete "/root/Desktop/testfile"
the program continue showing lines from file even the file don't exist anymore.
My problem is that i don't understand why it still prints lines after file deletion.
I run the program on CentOS 5. ( don't know if this is usefull).
Thanks in advance for any suggestion.