Thread: Help with struct... not returning correct results.

  1. #1
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15

    Help with struct... not returning correct results.

    This is the code I have. To check my struct I put in a printf function and instead of returning
    M 60 2
    F 42 1
    F 12 4
    M 26 3


    it's returning
    F 12 4
    1986850018 8M 26 3
    0 0


    Code:
    #include <stdio.h>
    #define MAXRECORDS 4
    
    struct record
    {
       char gender;
       int age;
       int lead;
    };
    
    void read_file();
    
    int main()
    {
       read_file();
    }
    
    void read_file()
    {
       FILE *fpin;
       int i;
       struct record rec[MAXRECORDS];
    
       fpin = fopen("custsurvey.dat", "r");
    
       if(fpin == NULL)
       {
          printf("Error opening file");
       }
       else
       {
          while(!feof(fpin))
          {
             for(i = 0; i < MAXRECORDS; i++)
                fscanf(fpin, "%c  %d %d", &rec[i].gender, &rec[i].age, &rec[i].lead);
          }
       }
       for(i = 0; i < MAXRECORDS; i++)
          printf("%c  %d %d", rec[i].gender, rec[i].age, rec[i].lead);
    
       fclose(fpin);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got a for loop inside your while loop; so you'll read 0,1,2,3; and then if feof is false (which it is) you'll do it again, so you'll read 0, 1, 2, 3 and overwrite. (As you've noticed, every other read is going wrong, probably because it's reading ... something in for %c and then not matching either of the %d's, which means they get garbage values. In this case, "something" might be a new-line or carriage-return character.)

    Edit: Oh, forgot to mention: feof will be false the first time through even if you were reading correctly, because feof doesn't get set to true until you try to read more data that isn't there. Also, the reason we believe your %c is picking up a new line is that new lines were printed in your output, despite the fact that they don't appear in your program.
    Last edited by tabstop; 01-18-2009 at 04:54 PM.

  3. #3
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    Ok how would I stop it reading in the newline character?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Put a space in your format string before the %c. (Perhaps contrary to expectations, and perhaps contrary to everything else, spaces don't literally match spaces in your input. Any whitespace in a format string causes *scanf to ignore whitespace before reading the next conversion.)

  5. #5
    Registered User drty2's Avatar
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    15
    Awesome thanks, work's now :-)

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Put a space in your format string before the %c. (Perhaps contrary to expectations, and perhaps contrary to everything else, spaces don't literally match spaces in your input. Any whitespace in a format string causes *scanf to ignore whitespace before reading the next conversion.)
    If it were an float or double would the space still be necessary? I tried with that problem. I new it was the fscanf function causing trouble, once the while loop was eliminated but i googled and found nothing to support that space before %c. It works. Guess some things come with experience.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by fread View Post
    If it were an float or double would the space still be necessary? I tried with that problem. I new it was the fscanf function causing trouble, once the while loop was eliminated but i googled and found nothing to support that space before %c. It works. Guess some things come with experience.
    You need to get way better with googling, then. I'm not aware of any source that doesn't say something like
    Quote Originally Posted by man scanf
    Code:
         c     Matches a sequence of width count characters (default 1); the next
               pointer must be a pointer to char, and there must be enough room
               for all the characters (no terminating NUL is added).  The usual
               skip of leading white space is suppressed.  To skip white space
               first, use an explicit space in the format.
    or
    Quote Originally Posted by also man scanf
    White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself.
    But %f and %d skip white space by default (as you can see in the discussion of %c above, it is unusual in that respect).

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not use feof to control loop - read FAQ
    use return value of fscanf for this purpose
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM