How to do a while loop until fscanf hits a string?

This is a discussion on How to do a while loop until fscanf hits a string? within the C Programming forums, part of the General Programming Boards category; Code: while (EOF != fscanf(fp,"s", name)) { matches = 0; //count = 0; final = 0; while ((c = getchar()) ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    How to do a while loop until fscanf hits a string?

    Code:
    while (EOF != fscanf(fp,"s", name)) { 
            matches = 0;
            //count = 0;
            final = 0;
            while ((c = getchar()) != '\n')) { 
                    matches = matches + 1;
                   while (1) {      
                        fscanf(fp, "%d %d %d %d %d %d", &s1,&s2,&s3,&s4,&s5,&s6);
                        //s3total = s3total + s3;
                        average = (s1+s2+s3+s4+s5+s6)/6;
                       totalavg = totalavg + average;
                        break;
                    }
             }
    ...
    Basically, My program is reading a data file with a Name, and n sets of 6 scores, and then averaging all those scores. I want my first while loop to scan a name, and then have the second while loop loop until it hits a new line, meaning a new name.

    This is the portion of my program that I've been stuck on.. Thanks for reading!

    This is a portion of the actual data file.
    Code:
    Arribas        10 6  2  1  4  0        6  3  1  8  7  5  
    Sanchez        7  4  9  10 2  0        10 8  5  0  4  6  
    Rooney        10 3  10 10 7  10       3  7  9  7  8  2        10 7  10 4  1  0  
    Messi        4  9  7  6  8  6        2  2  7  6  6  5  
    Lionel        7  4  4  4  3  6        9  10 2  4  1  0        10 9  4  0  10 1        2  6  9  7  1  4        9  3  9  2  10 3
    and etc.

    I believe my c = getchar() is one of the causes of my problems, because I get an infinite loop from it. oh and, I am a beginner :T.
    Last edited by d1puffpuff; 10-28-2011 at 02:06 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,850
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
      char name[100];
      char *p = buff;
      int pos;
      sscanf(p,"%s%n", name, &pos );
      p += pos;
      // now do the same thing in a loop, reading 1 integer at a time until the
      // end of the string.
      // If you've never seen %n before, look it up.
      // an alternative is to use strtol() which can also advance a char pointer
      // through a string of ints
    }
    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
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    I don't know what you were trying to do with your getchar loop, but basically you consume all characters between the name and the end of the line. Then, you enter what should be an infinite loop, but you break out of it on the first iteration, so that's not your infinite loop. The infinite loop is from your outer-most while loop. You're using "s" as a format string, which looks for a literal "s", and stores nothing in name. Since it doesn't find that s at the first character, it doesn't read anything from the file and you never get an EOF. You need a % sign for your format specifier, "%s". Also, fscanf returns the number of successfully converted items. You want to convert one string, so you should check if fscanf returns 1 there, not EOF. Similarly, you should put the fscanf for the 6 ints in the loop condition, and check that it returns 6. Something like the following:
    Code:
    while (fscanf(fp, "%s", name) == 1) {
        while (fscanf(fp, "%d %d %d %d %d %d", &s1, &s2, &s3, &s4, &s5, &s6) == 6) {
        }
    }

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Quote Originally Posted by anduril462 View Post
    I don't know what you were trying to do with your getchar loop, but basically you consume all characters between the name and the end of the line. Then, you enter what should be an infinite loop, but you break out of it on the first iteration, so that's not your infinite loop. The infinite loop is from your outer-most while loop. You're using "s" as a format string, which looks for a literal "s", and stores nothing in name. Since it doesn't find that s at the first character, it doesn't read anything from the file and you never get an EOF. You need a % sign for your format specifier, "%s". Also, fscanf returns the number of successfully converted items. You want to convert one string, so you should check if fscanf returns 1 there, not EOF. Similarly, you should put the fscanf for the 6 ints in the loop condition, and check that it returns 6. Something like the following:
    Code:
    while (fscanf(fp, "%s", name) == 1) {
        while (fscanf(fp, "%d %d %d %d %d %d", &s1, &s2, &s3, &s4, &s5, &s6) == 6) {
        }
    }
    yeah... getchar wasn't exactly what I was looking for..
    @Salem: fgets( was really useful here, and I'm thankful for the tip
    @anduril462: equating the fscanf to 1 and 6 helped a lot as well. ^^
    Well, my program is fine now! thank you both! Both of your guys' input really helped!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf printf while loop help
    By BrandNew in forum C Programming
    Replies: 2
    Last Post: 03-10-2011, 03:10 AM
  2. Replies: 2
    Last Post: 09-13-2009, 04:14 PM
  3. reading int and string from file fscanf
    By awesmesk8er in forum C Programming
    Replies: 5
    Last Post: 02-12-2009, 04:01 PM
  4. fscanf in loop
    By pshirishreddy in forum C Programming
    Replies: 4
    Last Post: 11-08-2008, 05:50 AM
  5. Fscanf endless loop
    By krum in forum C Programming
    Replies: 3
    Last Post: 12-05-2007, 12:14 AM

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