#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0

    fscanf not reading what was asked


    Aloha! I'm getting an error while using fscanf - I use it to read a file that contains many data on a line, but I only need the first two parts of it, a string and an integer; so the syntax I'm using is fscanf(fp,"%s %d\n", name, &number);
    Problem is, it keeps on reading to the end of the line, instead of changing line after reading the integer! What's wrong with this?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    I suggest that you also include some lines from the file, that you're trying to read. That will help us debug your problem.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0
    Here's the file:

    word 2 19-05-2002 14:30:31
    word 4 19-05-2002 14:30:35
    word 2 19-05-2002 14:30:37
    cancel 2 19-05-2002 14:30:39
    cancel 3 19-05-2002 14:30:42
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    turkey
    Posts
    15
    Rep Power
    0
    do not use "\n" with scanf or fscanf
    it may help u
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,183
    Rep Power
    2222
    bass20:
    As I've already demonstrated, I'm not expert on the scanf functions, but you can use the * to tell scanf to parse a field but not assign it to a variable. So you should be able to write a format string for the entire line, but only have it save the ones you want.

    Another possibility might be to use a different function, eg fgets(), to read an entire line from the file and store it in a string, then run sscanf on that string. That should work, though you will not have learned how to solve the original problem.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0
    OK, I cheated around it :D

    while( !feof(fp) ){
    fscanf(fp,"%s %d\n", palavra, &duracao);
    fgets( trash, sizeof(trash), fp );
    /*etc...*/

    ...and fgets will "garbage collect" the rest of the line


    Thanx for the attention :)

IMN logo majestic logo threadwatch logo seochat tools logo