Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Anything wrong with this code?

Reply
Thread Tools

Anything wrong with this code?

 
 
webinfinite@gmail.com
Guest
Posts: n/a
 
      08-16-2007
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?

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      08-16-2007
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:

>#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.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-16-2007
(E-Mail Removed) <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-17-2007
"(E-Mail Removed)" 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there anything fundamentally wrong with this code? simon.stockton@baesystems.com VHDL 5 04-20-2006 01:09 PM
brut force eigrp over gre working, is there anything wrong? jcharth@hotmail.com Cisco 0 10-05-2005 09:57 PM
not receiving email,is anything wrong? =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 3 07-28-2004 10:32 PM
Is there anything wrong with my insert code? Mano kumar ASP .Net 2 10-17-2003 01:51 PM
Re: Is there anything wrong... rf HTML 1 07-06-2003 11:29 AM



Advertisments