473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error checking using sscanf

Im using sscanf to scan in a number of run time arguments and convert the strings to ints.It works fine when all the arguments are ints but wont detect when the arguments supplied are words instead of numbers.It just seems to ignore them.

I want it to detect when its not supplied a valid string (eg 3 is ok but three is wrong), and then output an error message, however I dont know how to detect this error when using sscanf.

Any help is appreciated.
May 8 '07 #1
3 4510
weaknessforcats
9,208 Expert Mod 8TB
scanf() can't detect anything. All it can do is process what you tell is. That is, if you use %s YOU MUST have a string. If you use %d YOU MUST have an int.

You will have to use getch() to get a character. Test it and decide what to do and then ungetch() to put it back in the inoutbuffer before you continue.
May 8 '07 #2
AdrianH
1,251 Expert 1GB
Im using sscanf to scan in a number of run time arguments and convert the strings to ints.It works fine when all the arguments are ints but wont detect when the arguments supplied are words instead of numbers.It just seems to ignore them.

I want it to detect when its not supplied a valid string (eg 3 is ok but three is wrong), and then output an error message, however I dont know how to detect this error when using sscanf.

Any help is appreciated.
For this case you can use the return value which is how many of the arguments have been assigned to a variable.
Expand|Select|Wrap|Line Numbers
  1. int value;
  2. printf("Variables assigned: %d\n", scanf("%d", &value));
  3.  
INPUT: 3
OUTPUT: Variables assigned: 1

INPUT: three
OUTPUT: Variables assigned: 0


Adrian
May 8 '07 #3
Banfa
9,065 Expert Mod 8TB
Or you could use the functions strtoul or strtol (Google them for a definition).

These function skip leading space and convert any digits to a unsigned long or long and then return a pointer to the character the conversion stopped on allowing you to tell what caused the conversion to halt. If it was a letter then you can raise an error of some sort.

For a string with multiple integers in it just call the function multiple times until you get to the terminator (or an error).
May 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
2
by: Eric Twietmeyer | last post by:
Hello, I am running into something very strange. I am writing a .NET assembly using managed c++ (VS.NET 2003) and in this project when I attempt to make a call to sscanf (from stdio.h) I get...
9
by: GrispernMix | last post by:
bool variant_t::Convert( fieldtype_t newType ) { if ( newType == fieldType ) { return true; } // // Converting to a null value is easy. //
33
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now...
15
by: H. | last post by:
What's the easiest way to check if an argument entered at the command line is a number? I know that one way would be to treat the argument as a character string, and then manually check each...
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
12
by: jonatan | last post by:
Hello All, I am making a program and need the grt_main.c but i try to compile have the error c1004.Please help me how to solve it? Thank you. --------------------Configuration: pre - Win32...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
2
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.