Thread: Check the return status of scanf

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Check the return status of scanf

    This is my code I'm not sure where I've gone wrong or how I do it but I keep getting this message back... Any help would be great.


    75. fscanf(fp, "%d", &check);
    ==> You have not checked the return status of scanf.

    90. fscanf(fp, "%f", &hour);
    ==> You have not checked the return status of scanf.



    Code:
          fscanf(fp, "%d", &check);
          if (feof(fp))
          {
             fclose(fp);
             break;
          }
          fscanf(fp, "%s", stay);
          if (feof(fp))
          {
             fclose(fp);
             break;
          }
          fscanf(fp, "%s", taken);
          if (feof(fp))
          {
             fclose(fp);
             break;
          }
          fscanf(fp, "%f", &hour);

  2. #2
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Maybe it is because since you have closed your file you can't read from it.
    For example if the first fscanf succeeds the other won't work.You must fix your code.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Why u r using these many of 'if's' for the same purpose in ur program??
    S_ccess is waiting for u. Go Ahead, put u there.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Message just says what it says - you don't check the return value:
    Code:
    int count = fscanf(fp,"%d",&value);
    if(count != 1)
    {
       //Hey - we have problem with reading from the file
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fscanf(fp, "%d", &check);
    Meaning it should be

    if ( fscanf(fp, "%d", &check) == 1 )
    // it was successful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Quote Originally Posted by maven
    Why u r using these many of 'if's' for the same purpose in ur program??
    How would you recommend doing it?

    Thanks for the help on the returning scanf I realise my mistake now.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Since || is short-cutting, you could do something like this:
    Code:
    if(fscanf(fp, "%d", &check) != 1 || fscanf(fp, "%s", stay) != 1
        || fscanf(fp, "%s", taken) != 1 || fscanf(fp, "%f", &hour) != 1) {
    
        /* failed */
    }
    Better yet:
    Code:
    if(fscanf(fp, "%d%s%s%f", &check, stay, taken, &hour) != 4) {
        /* failed */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I tried the second one originally but it gives me a tonne of syntax errors and the first example causes and error in the program (the program freezes) when it tests weather the file has a corrputed float.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    thats cause you havent provided the rest of the code. The error is there probably

    Im willing to bet you have something like this:

    Code:
    char *stay;
    fscanf(fp, "%s", stay);
    correct?

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Fixed it... Thanks
    Last edited by Taka; 11-01-2006 at 02:43 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Nice to see you've been listening

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
      if ( sscanf( buff, "%d %s %s %f", &check, stay, taken, &hour ) == 4 ) {
        // Now do your linked list bit
      } else {
        // some error?
      }
    }
    > newnode = (Node*)malloc(sizeof(Node));
    Read the FAQ on casting malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM