How do you validate sscanf?

This is a discussion on How do you validate sscanf? within the C Programming forums, part of the General Programming Boards category; I'm trying to fail when the string being parsed is 1. second field in string is too big, 2. Expected ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    How do you validate sscanf?

    I'm trying to fail when the string being parsed is 1. second field in string is too big, 2. Expected float is not a float, 3. more than 2 fields in string. What am I dong wrong?
    Code:
    static int parse_sscanf( const char *in_str, struct parse_s *parse_struct )
    {
    
    	char buffer[BUFFSIZE];
    	int inx;
    	strncpy(buffer, in_str, BUFFSIZE - 1);
    	buffer[BUFFSIZE - 1] = '\000';
    
    	if (inx = sscanf( buffer, "%lf%*c%s", &parse_struct->value, parse_struct->name ) != 2 )
    	{
    		return FALSE;
    	}
    
    	else
    
    	return TRUE;
    }
    ouput...
    Success parsed >>123.456 some-number-name<< to >>some-number-name<< and 123.456000
    Success parsed >> 314.159 pi*100<< to >>pi*100<< and 314.159000
    Success parsed >>314159 pi*100000<< to >>pi*100000<< and 314159.000000
    Success parsed >>0.314159 pi/10<< to >>pi/10<< and 0.314159
    Success parsed >>123.45 123.45 << to >>123.45<< and 123.450000
    Success parsed >>314.159 pi*100 << to >>pi*100<< and 314.159000
    Success parsed >>-123.456 a-neg-number << to >>a-neg-number<< and -123.456000
    Success parsed >>0.0 zero-number<< to >>zero-number<< and 0.000000
    Success parsed >>0 another-zero<< to >>another-zero<< and 0.000000
    Success parsed >>123.456 some-really-long-name-string<< to >>some-really-long-name-string<< and 123.453150 (should have failed)
    Success parsed >>3.1.4159 pi<< to >>4159<< and 3.100000 (should have failed)
    Success parsed >>499.999 some-number 299.999 extra << to >>some-number<< and 499.999000 (should have failed)
    Failed to parse >><<
    Failed to parse >>no-num<<
    Failed to parse >>invalid-num xxx<<
    Failed to parse >>name-in-wrong-position 123.45<<
    Failed to parse >>123.45 <<

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Wrap parenthesis around your assignment, and compile with warnings on:
    Code:
    if( (inx = sscanf( buffer, "%lf%*c%s", &parse_struct->value, parse_struct->name )) != 2 )
    Start with that.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The most convenient method would probably be to use %n - which gives you the number of characters converted so far. So if you put a %n at the end of the normal input, you should get a number matching the length of the string [give or take, if there are spaces on the end of the string, you may not get the spaces processed - so you may have to either strip trailing spaces or check if the remaining part of the string is only whitespace].

    By the way,
    Code:
    	if (inx = sscanf( buffer, "%lf%*c%s", &parse_struct->value, parse_struct->name ) != 2 )
    doesn't do what I think you think it does. It assigns inx with
    Code:
    (sscanf( buffer, "%lf%*c%s", &parse_struct->value, parse_struct->name ) != 2 )
    , which is either 0 (if sscanf returned something other than 2) or 1 (if sscanf returns 2). You probably expected to get 2 out of inx when the input is consumed correctly. To do that, you would have to add extra parenthesis to make the assignment happen before the check of not-equal-to 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An equivalent of sscanf
    By g4j31a5 in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2007, 01:27 AM
  2. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  3. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  4. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 03:53 PM

Tags for this Thread


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