"" wrote:
>
.... 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.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from
http://www.teranews.com