Anything wrong with this code?

Discussion in 'C Programming' started by webinfinite@gmail.com, Aug 16, 2007.

  1. Guest

    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?
     
    , Aug 16, 2007
    #1
    1. Advertising

  2. In article <>,
    <> 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
     
    Walter Roberson, Aug 16, 2007
    #2
    1. Advertising

  3. <> 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 ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Aug 16, 2007
    #3
  4. CBFalconer Guest

    "" 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
     
    CBFalconer, Aug 17, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mano kumar
    Replies:
    2
    Views:
    360
    Kevin Spencer
    Oct 17, 2003
  2. Replies:
    5
    Views:
    508
  3. Chris
    Replies:
    7
    Views:
    470
    BigBrian
    Feb 9, 2005
  4. jim

    anything wrong with this code?

    jim, Aug 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    312
    Mark A. Odell
    Aug 26, 2004
  5. hijkl
    Replies:
    28
    Views:
    803
    Lionel B
    Mar 8, 2007
Loading...

Share This Page