Thread: Loop problem...Is is possible to delete a line of printed text??

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    13

    Loop problem...Is is possible to delete a line of printed text??

    I wrote a program that reads in a file into an array of structures. I am supposed to check that the data is valid while reading it in. It should read in 7 elements into the structure and print an Invalid Data message if anything besides 7 elements are read in correctly. The program has a loop that should break when the end of the file is reached but for some reason it still loops one more time and therefore prints a false Invalid Data statement. I can't figure out the logic to change my loop so this will not happen. Crazy idea but can i just somehow delete that last Invalid Data statement somehow??? Or how can I change the loop?

    Here is the code
    Code:
    //open file
    fp = fopen(argv[1], "r");  
    if (fp == NULL)
      {
      printf("Error: Could not open file for read.\n\n");
      return EXIT_FAILURE;
      } //if
    
    //now open and take care of fscanf with : between fields
    while ((fgets(buffer, 1000, fp) != NULL) && i < 100)
            {
              result = sscanf(buffer, "%20[A-za-z ,]:%6[0-9]:%d:%d:%d:%d:%s", stuAry[i].name, stuAry[i].studentID, &stuAry[i].scoreOne, &stuAry[i].scoreTwo, &stuAry[i].scoreThree, &stuAry[i].scoreFour,
    stuAry[i].grade);
     
     
              if (result == EOF)
              {
               break;
                    } /* close if */
     
                    else if (result != 7)
                            {
                              printf("Invalid Record: %s", buffer);  
                                    } /* close second if */else if (result == 7)
                              {
                                    sum = (stuAry[i].scoreOne + stuAry[i].scoreTwo + stuAry[i].scoreThree + stuAry[i].scoreFour);
                                    stuAry[i].average = (sum / 4);
                                    i++;
                                } //if
             } //while
    and when I run the program I get this

    Code:
    Invalid Record: Data, Bad:9876543:100:34:54:21:F
    Invalid Record: Data, Missing:123456:100::99:50:C
    Invalid Record:
    How can I get rid of that last line?

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure there's not a blank line in your input? You appear to be doing the file handling correctly.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your current problem can be fixed by checking your result returned from sscanf(), and when the result is < 2, break.

    That will stop the newline at the end of the file, from causing a misprint.

    This is an error you are not troubled with, yet:

    Code:
    sscanf("&#37;[A-za-z ,] etc.
    
    
    //should be:
    
    sscanf("%[A-Za-z ,] etc.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    13
    alright thanks guys

    And Adak thanks nice catch

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And to emphasize what Adak says: sscanf() is most likely NEVER going to return EOF.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also, your code might be a little more maintainable and easier to read if you processed each number on a line in a loop, with something like this.
    Code:
    char buffer[BUFSIZ];
    // read buffer
    char *start = buffer;
    int add;  // how much data was processed
    int number, sum = 0;
    
    while(sscanf(start, "&#37;d%n", number, &add) == 2) {
        sum += number;
        start += add;
    }
    
    // now sum is the sum of all of the numbers
    Just a thought. What you have works fine too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    while(sscanf(start, "%d%n", &number, &add) == 2) {
    ?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by matsp View Post
    Code:
    while(sscanf(start, "%d%n", &number, &add) == 2) {
    ?

    --
    Mats
    I'm not sure %n is counted by scanf...

    At least in the VS6 - it does not :

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int n,k;
    	int m = sscanf("100 200","%d%n", &n,&k);
    	printf("m=%d k=%d\n", m,k);
    	return 0;
    }
    Output:
    Code:
    m=1 k=3
    Press any key to continue
    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

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    From a man page:
    The C standard
    says: `Execution of a &#37;n directive does not incre-
    ment the assignment count returned at the comple-
    tion of execution' but the Corrigendum seems to
    contradict this. Probably it is wise not to make
    any assumptions on the effect of %n conversions on
    the return value.
    So it would probably be best to use "%d%n" and make sure the return value is >= 1.

    Yeah, and I made a mistake in omitting that ampersand.

    Anyway, strtol() is much easier to use in this case in my opinion anyway.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Problem with signals
    By kahad in forum C Programming
    Replies: 9
    Last Post: 12-07-2006, 10:42 AM
  3. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 04:41 PM
  4. 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
  5. text line termination
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-09-2001, 04:39 AM