W
webinfinite
I am writing a code to read a file. The file say "foo.txt" is not
generated at the first place. So the code will sit in a tight loop to
wait this file being generated from the 3rd party. I don't concern the
3rd party generation as long as the file is generated eventually.
The code is:
#include <stdio.h>
#include <stdio.h>
int main(){
FILE *fp;
while(1){
fp = fopen("foo.txt", "r"); ;
if (fp != NULL)
break;
else{
printf("Waiting for the file\n");
}
fclose(fp);
}
printf("I am out.\n");
fclose(fp);
return 0;
}
The execution result is:
Waiting for the file
Segmentation fault (core dumped)
What is wrong?
generated at the first place. So the code will sit in a tight loop to
wait this file being generated from the 3rd party. I don't concern the
3rd party generation as long as the file is generated eventually.
The code is:
#include <stdio.h>
#include <stdio.h>
int main(){
FILE *fp;
while(1){
fp = fopen("foo.txt", "r"); ;
if (fp != NULL)
break;
else{
printf("Waiting for the file\n");
}
fclose(fp);
}
printf("I am out.\n");
fclose(fp);
return 0;
}
The execution result is:
Waiting for the file
Segmentation fault (core dumped)
What is wrong?