can fscanf skip reading data conditionally?

Discussion in 'C Programming' started by John, Sep 27, 2006.

  1. John

    John Guest

    I need to read data from the file like the following with name and
    score, but some line may only has name without score:

    joe 100
    amy 80
    may

    Here's my code, but it couldn't read the line with "may" because there
    is no score. Anyone knows what is the workaround to this problem?

    char name[20];
    int score;

    while (fscanf(ifp, "%s %d", name, &score) == 2)
    {
    printf("%s %d\n", name, score);
    }


    please advice. thanks!!
     
    John, Sep 27, 2006
    #1
    1. Advertising

  2. "John" <> wrote in message
    news:...
    >I need to read data from the file like the following with name and
    > score, but some line may only has name without score:
    >
    > joe 100
    > amy 80
    > may
    >
    > Here's my code, but it couldn't read the line with "may" because there
    > is no score. Anyone knows what is the workaround to this problem?
    >
    > char name[20];
    > int score;
    >
    > while (fscanf(ifp, "%s %d", name, &score) == 2)
    > {
    > printf("%s %d\n", name, score);
    > }
    >


    What makes you think it couldn't read the line?
    I'll bet it did it just fine, and returned 1.

    >
    > please advice. thanks!!
    >

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
     
    Fred Kleinschmidt, Sep 27, 2006
    #2
    1. Advertising

  3. John wrote:
    > I need to read data from the file like the following with name and
    > score, but some line may only has name without score:
    >
    > joe 100
    > amy 80
    > may
    >
    > Here's my code, but it couldn't read the line with "may" because there
    > is no score. Anyone knows what is the workaround to this problem?
    >
    > char name[20];
    > int score;
    >
    > while (fscanf(ifp, "%s %d", name, &score) == 2)
    > {
    > printf("%s %d\n", name, score);
    > }


    int i;
    while ((i = fscanf(ifp, "%s %d", name, &score)) >= 1)
    {
    if (i == 2)
    printf("%s %d\n", name, score);
    else
    printf("%s\n", name);
    }

    Robert Gamble
     
    Robert Gamble, Sep 27, 2006
    #3
  4. John

    Michael Mair Guest

    John wrote:
    > I need to read data from the file like the following with name and
    > score, but some line may only has name without score:
    >
    > joe 100
    > amy 80
    > may
    >
    > Here's my code, but it couldn't read the line with "may" because there
    > is no score. Anyone knows what is the workaround to this problem?
    >
    > char name[20];
    > int score;
    >
    > while (fscanf(ifp, "%s %d", name, &score) == 2)
    > {
    > printf("%s %d\n", name, score);
    > }


    You did not specify what you want to do if you encounter this problem.
    One example:

    char name[20];
    int score;
    int ret;

    while (EOF != (ret = fscanf(ifp, "%19s %d", name, &score)))
    {
    if (ret > 0) {
    printf("%s", name);
    if (ret > 1) {
    printf("%d", score);
    }
    }
    putchar('\n');
    }

    Next time, explain what you want to do, what you have, how the
    results of your code do not match what you expected, and give
    a compilable minimal example. This helps us help you.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Sep 27, 2006
    #4
  5. "Robert Gamble" <> writes:
    > John wrote:
    >> I need to read data from the file like the following with name and
    >> score, but some line may only has name without score:
    >>
    >> joe 100
    >> amy 80
    >> may
    >>
    >> Here's my code, but it couldn't read the line with "may" because there
    >> is no score. Anyone knows what is the workaround to this problem?
    >>
    >> char name[20];
    >> int score;
    >>
    >> while (fscanf(ifp, "%s %d", name, &score) == 2)
    >> {
    >> printf("%s %d\n", name, score);
    >> }

    >
    > int i;
    > while ((i = fscanf(ifp, "%s %d", name, &score)) >= 1)
    > {
    > if (i == 2)
    > printf("%s %d\n", name, score);
    > else
    > printf("%s\n", name);
    > }


    fscanf() skips whitespace, including newlines. If you call fscanf
    with a format of "%s %d", it will read a blank delimited word, then
    look for a decimal integer; if there isn't one on the current line, it
    will continue reading lines until it finds either a decimal integer or
    something that definitely isn't one.

    That might happen to work, but it's fragile. Also, it won't detect
    an error such as:

    joe 100
    amy 80
    may

    42

    fred 97

    (I'm assuming you want each name and optional score to be on a single
    line.)

    A better solution is to read an entire line at a time (use fgets() if
    you can assume a maximum line length) and then apply sscanf() to the
    resulting string.

    An advantage of sscanf() is that it doesn't consume its input; you can
    pass the same string to it again with a different format. Another
    advantage is that, if the string contains a single line of input, it
    won't try to read additional lines; it treats the end of the string
    like end-of-file.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 27, 2006
    #5
    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. Benedicte

    Problems when reading from a file with fscanf

    Benedicte, Feb 4, 2004, in forum: C Programming
    Replies:
    3
    Views:
    458
    Al Bowers
    Feb 4, 2004
  2. fscanf reading lines

    , Mar 8, 2006, in forum: C Programming
    Replies:
    7
    Views:
    910
    Fred Kleinschmidt
    Mar 8, 2006
  3. Replies:
    7
    Views:
    484
    Default User
    Sep 5, 2006
  4. V.Subramanian, India

    no error by fscanf on reading from output file

    V.Subramanian, India, Oct 30, 2011, in forum: C Programming
    Replies:
    18
    Views:
    596
    Keith Thompson
    Oct 31, 2011
  5. Jie
    Replies:
    6
    Views:
    209
    Peter Makholm
    Jun 28, 2007
Loading...

Share This Page