annoying validation issue with sscanf

This is a discussion on annoying validation issue with sscanf within the C Programming forums, part of the General Programming Boards category; The code below works perfectly fine with test.txt: blah1 44.3 blah2 34.3 as soon as i add2 blank lines to ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    annoying validation issue with sscanf

    The code below works perfectly fine with

    test.txt:

    blah1 44.3
    blah2 34.3


    as soon as i add2 blank lines to the end of the text file

    else kicks in:

    Number of lines: 1
    Number of lines: 2
    line cannot be printed

    Is it possible, once the end of the file is reached to exit somehow and ignore how many blank lines there are at the end?

    [code]

    #include <stdio.h>
    #include <stdlib.h>


    int main()
    {
    FILE *fp;
    char name[25];
    char buff[BUFSIZ];
    float num;
    int nooflines = 0;

    if((fp=fopen("test.txt","r"))==NULL)
    {
    perror("File cannot be opened");
    exit(1);
    }

    while(fgets(buff,sizeof(buff),fp)!=NULL) // read a single line from the file till i get EOF
    {
    if(sscanf(buff,"%s %f",name,&num)==2) // getting the name and the number from the buffer which was read from the file
    {
    nooflines++;
    printf("%s %d", "\n Number of lines: ", nooflines);
    }
    else
    {
    printf("\nline cannot be printed\n");
    exit(0);
    }
    }

    return 0;
    }



    I've tried modifying the if statement to:
    Code:
     if(sscanf(buff,"%s %f",name,&num) == 2 || sscanf(buff,"%s %f",name,&num) != '\n' )
    it works

    but if i add crap to the
    text file:

    blah1 44.3
    dsds
    blah2 34.3

    it still reads it as a valid line (i.e. the second line in the text file is not valid it has no valid number). I tried changing the || to && but it gives me the same original problem i had.
    Last edited by Axel; 10-23-2005 at 09:29 AM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Sure, just use something like
    Code:
    if(!buff[0] || (!buff[1] && buff[1] == '\n')) continue;
    which will skip over blank lines (blank except for a new line).

    Is that what you meant?
    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.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    i did something simliar:

    Code:
     
    if(sscanf(buff,"%s %f",name,&num) != '\n' )
    it just prints out

    Number of lines: 1
    Number of lines: 2
    Number of lines: 3
    Number of lines: 4

    it doesn't seem to skip the blank lines i added 2 lines of text and 2 blank lines

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Comparing the return value of sscanf to '\n' doesn't make much sence.

    Return Value.
    The number of items succesfully read. This count doesn't include any ignored fields.
    If EOF is returned an error has occurred before the first assignation could be done.
    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.

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hmm you have a point there.

    i got it to work:

    Code:
      while(fgets(buff,sizeof(buff),fp)!=NULL)  // read a single line from the file till i get EOF
        {
    
            if(sscanf(buff,"%s %f",name,&num) != '\n' && sscanf(buff,"%s %f",name,&num) == 2 )  // getting the name and the number from the  buffer which was read from the file
                {
                   if(!buff[i] || (!buff[i] && buff[i] == '\n')) continue;
                   nooflines++;
                   printf("%s %d", "\n Number of lines: ", nooflines);
                   i++;
                }
            else
            {
                printf("\nline cannot be printed\n");
                exit(0);
            }
        }
    one prob though, for some reason "line cannot be printed even though i have no blank lines etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    sscanf(buff,"%s %f",name,&num) != '\n'
    Fix that!
    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
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    whoops, i actually tried it without that bit but it still prints out "cannot be printed" i have the correct format for sscanf.

    Code:
    while(fgets(buff,sizeof(buff),fp)!=NULL)  // read a single line from the file till i get EOF
        {
            if(!buff[i] || (!buff[i] && buff[i] == '\n')) continue;
            if(sscanf(buff,"%s %f",name,&num) == 2 )  // getting the name and the number from the  buffer which was read from the file
                {
                   nooflines++;
                   printf("%s %d", "\n Number of lines: ", nooflines);
                   i++;
                }
            else
            {
                printf("\nline cannot be printed\n");
                exit(0);
            }
        }

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Works for me with a test.txt file that contains string<space>floating point<newline> lines, what is your input?

  9. #9
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    blah1 44.3
    blah1 44.3

    there's two blank lines at the bottom

    Number of lines: 1
    Number of lines: 2
    line cannot be printed <- shouldn't be there :|

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Why shouldn't it?

    You are reading in each line, and you are using fscanf to read in a string and a double. Of course it's going to fail (not return 2) if you feed it a blank line instead of a string/double pair.

    What is it you actually expect to happen? If you want to ignore blank lines, check if the fgets result string only contains a newline, then skip your fscanf if that is the case.

  11. #11
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Yes, instad of it printing 'there's an error' i just want it to ignore empty lines are print nothing.

    i know what you mean with changing the fgets line, but i cant get it to compile 'operands of != have illegal types 'pointer to char' and 'char'

    Code:
        while(fgets(buff,sizeof(buff),fp)!=NULL && fgets(buff,sizeof(buff),fp)!='\n')  // read a single line from the file till i get EOF
        {
    
            if(sscanf(buff,"%s %f",name,&num) == 2)  // getting the name and the number from the  buffer which was read from the file
                {
                    if(!buff[i] || (!buff[i] && buff[i] == '\n')) continue;
    
                   nooflines++;
                   printf("%s %d", "\n Number of lines: ", nooflines);
                   i++;
                }
            else
            {
                printf("\nline cannot be printed\n");
                exit(0);
            }
        }

  12. #12
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Comparing the return value of fgets to '\n' is ridiculous. fgets returns a pointer, either NULL or the pointer passed. The only thing you should be comparing to '\n' is buff[0], ie the first character of the result string.

    And you should do this BEFORE, not after the sscanf, because you want to continue to the next line without reading it with sscanf.

  13. #13
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    you mean?
    Code:
    
    
        while(fgets(buff,sizeof(buff),fp)!=NULL)  // read a single line from the file till i get EOF
        {
          if(!buff[i] || (!buff[i] && buff[i] == '\n')) continue;
    
          if(sscanf(buff,"%s %f",name,&num) == 2)
          {
    
                   nooflines++;
                   printf("%s %d", "\n Number of lines: ", nooflines);
            }
            else
            {
                printf("\nline cannot be printed\n");
                exit(0);
            }
    
            i++;
    
    	}
    Number of lines: 1
    Number of lines: 2
    line cannot be printed

  14. #14
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    No.
    Code:
    if(!buff[i] || (!buff[i] && buff[i] == '\n')) continue;
    what on earth is that supposed to do? For a start, what is i? It makes no sense at all, and will always be false anyway. Why not just do:
    Code:
    if (buff[0] == '\n')
        continue;
    Simple.

  15. #15
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ahh so you actually meant buff[0] sorry, i misunderstood. I thought i had to increment the line as i work through the file. thanks.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  2. sscanf issue
    By DKING89 in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2008, 02:51 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 01:46 AM
  4. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 09:46 AM

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