Anything wrong with this code?

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?
 
W

Walter Roberson

#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;
}

If fp *is* NULL, then you do not break, so you fclose(fp)
which is fclose(NULL). That's not defined.
 
J

Jens Thoms Toerring

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);

This line is definitely not correct: you're not allowed to
call fclose() with a NULL pointer (and you can only get her
if 'fp' is NULL). Doing so invokes undefined behaviour and
getting a segmentation fault is one possible outcome.
}
printf("I am out.\n");
fclose(fp);
return 0;
}
The execution result is:
Waiting for the file
Segmentation fault (core dumped)

Regards, Jens
 
C

CBFalconer

.... snip ...

#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?

(I removed some extraneous braces.) It is not legal to pass NULL
to fclose. The NULL signifies that fopen failed. The scheme will
probably fail anyhow.

You might try for a considerable reduction in paper/terminal
display space with a simpler overall code in a function:

putc('\n');
while (! (fp = fopen("foo.txt", "r")) {
printf("\rWaiting for the file"); fflush(stdout);
/* optional delay - allow other things to run */
}
printf("\nGot it\n");
return fp;

and call that with:

fp = awaitfile(...);
/* play with file */
fclose(fp);

but worry about how to abort.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top