Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sscanf problem 1

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
I want to test the "time_str" to see if it's has the correct format. The format should be "##,##,##"

int hr, min, sec;
if sscanf( time_str, "%i %*[,] %i %*[,] %i", &hr, &min, &sec) !=3)

If a user enters for "time_str" something like:

02x,05,05 or 02,05x,05

the conversion will fail based on the rules in the sscanf(which is good!).

The problem I'm having is if the user enter something like this in the third position (i.e. sec):
02,05,05x <-- bad data in the 3rd position

the sscanf will be successful. How can I catch the fact that non-numeric data is in the 3rd position?

PS - I know I could probably switch over to using char definitions for the hr, min, sec and then test etc., but is there a way to sscanf this the way I have it presented?
Thanks!
 
If you put %n at the end of the format string, and supply an additional pointer to an int, then it will tell you how many characters have been processed.
Code:
int hr, min, sec;
int pos;
if ( sscanf( time_str, "%i %*[,] %i %*[,] %i%n", 
             &hr, &min, &sec,&pos) !=3) {
  // now validate the tail of the string
  if ( time_str[pos] .... ) {
  }
}

Note that %n does not bump up the conversion count, so it's still 3.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
That worked great! I've finally have a use for the %n conversion character!!

if (pos == strlen(time_str)) ...

thanks Salem!!
 
hmmm, wait a minute - I'm still not out of the woods.

If the time_str contains "02,05,0x17" , the sscanf is incorrectly parsing the "0x17" as a hex (23).



 
I'm just wondering... What is this input used for and why would anyone enter 0x17 in the first place?
What if the user enters "123456789,456789123,789123456" or "-1,-2,-3"...?
 
> the sscanf is incorrectly parsing the "0x17" as a hex (23).
Use %d if you only want decimal integers.
The %i format understands the hex and octal prefixes.

Also, as cpjust indicates, sscanf() cannot detect numeric overflow.
Look up strtol() and it's relatives if you also care about overflow.

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Yep, I've already caught/figured to use %d ... it now works completely.

FYI -
Via a command line argument, the user input needs to be in the format "hh,mm,ss" (with the commas). If the sscanf is successful (only integers and commas are supplied), the 3 components are then individually checked to see if they are within their respective range (hr >0 && hr <= 23; min and sec >=0 <=59).

So it's not a question of "why someone would enter "0x17", but rather a method to handle it in the unlikely event it does occur.

Thanks for all help Salem & cpjust ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top