fscanf problem

This is a discussion on fscanf problem within the C Programming forums, part of the General Programming Boards category; Hi I am new to C programming and I'm having some problems reading in data from a file and storing ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3

    Unhappy fscanf problem

    Hi I am new to C programming and I'm having some problems reading in data from a file and storing the information read in in a self-defined structure.

    The data which is stored in he file is formatted as follows:

    #<space>Name:<space>1\n
    #<space>Name:<space>2\n

    etc...
    The structure I have created is very simple:

    ----------------------------------------
    typedef struct{
    int id;
    char name[128];
    } TYPE;
    ----------------------------------------
    When reading in I want to ignore the hash symbol and the space at the beginning of each line, then store the name in the structure's 'name' field. Then ignore the colon character and the space which appears after the name and store the integer value in the 'id' field.

    I have an array which I have defined as:

    TYPE types[4] /*since there are 4 lines in the file which need reading in.*/

    I then loop and read in (what I think is) each line at a time:

    --------------------------------------------
    int i=0;
    while(i<4)
    {
    fscanf("%*[# ]%[a-zA-Z[-]]%*[: ]%d\n", &types[i].name, &types[i].id);
    printf("%d %s\n", types[i].id, types[i].name);
    i++;
    }
    --------------------------------------------

    This compiles fine on the gcc compiler however, the problem that I get is that the output which is printed each time I go through the loop is wrong. I think it's probably my format string which is incorrect, but I have looked at numerous text books and online sources and I couldn't find any useful examples.

    I would be greatfull if anybody could point me in the right direction because I don't understand why it isn't working correctly. I have tried altering my format string and I sometimes get:

    0 String1
    0
    0

    and other times I get:

    1
    2
    3

    (where no names are shown)

    I hope I have described this problem clearly enough - If not please let me know.

    Any help would be much appreciated. Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    1. Read the line into a buffer using fgets()
    2. Parse the line using sscanf()

    Code:
    char buff[BUFSIZ];
    while ( i < 4 && fgets( buff, sizeof buff, fp ) != NULL ) {
      if ( sscanf( buff, "# %[^:]: %d", types[i].name, &types[i].id ) == 2 ) {
        i++;
      }
    }
    Remember, characters also stand for themselves in a scanf type line, so this scanf call will begin with trying to match a #
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Hmm, perhaps "#%[^:]:%d"? When matching literal values in the format string, you don't need to use a format modifier to find and discard them. Just put them right in the string and scanf will attempt to match that character. Also keep in mind that aside from %c, %n, and %[, all of the format modifiers will skip leading whitespace.

    [edit: ah, too slow]
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3
    Thanks very much for the speedy replies
    Apologies for not using the code tags (as well as being new to C I am a new member to this forum)! I will have another go at the techniques you have suggested.

    Thanks again!

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3
    Thank you both very much! I managed to get it working It turned out that my format string was infact incorrect. Changing it to
    Code:
    fscanf(file, "#%[^:]:%d\n", &types[i].name, &types[i].id);
    worked. Thanks for the heads-up on how format strings work! This has been bugging me for several days now!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 4
    Last Post: 01-10-2006, 12:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21