How do we check the return values ​​from the scanf () function?

I want to confirm if the value returned by the scanf () function is a floating number or not. How can i do this? My code doesn't work as it should if the wrong data types are passed to the scanf () function. similarly, how would I confirm if the return value is a character string or not?

+3


source to share


2 answers


scanf()

and others return the number of successful conversions.

If you have:

scanf("%f", &f);

      

you should check:

if (scanf("%f", &f) == 1)
    ...all OK...
else
    ...EOF or conversion failure...

      

If you have multiple conversions, make sure they are all complete. If you use %n

"conversions", they don't count.

Although it scanf()

returns EOF to EOF, you shouldn't be testing this & mdash; you should always check, first of all, that you have received the number of conversions that you expected. For example, consider the error code:



while (scanf("%f %d %s", &f, &i, s) != EOF)  // Here be BUGS!
    ...loop body...

      

If you type 3.14 x23 yes

, then you have an infinite loop, because it scanf()

will return 1 on the first iteration (it successfully converted 3.14) and 0 after that (not EOF).

You might be fine:

while ((rc = scanf("%f %d %s", &f, &i, s)) != EOF)
{
    if (rc != 3)
        ...oops data problems...
    else
        ...all OK...
}

      


Judging from the previous questions, you should use fgets()

(or maybe POSIX getline()

) to read lines of data and then use sscanf()

or even functions like strtol()

and strtod()

to read specific values ​​from the line. If you use sscanf()

, the comments made above about checking the number of successful conversions still apply.

I am not using scanf()

in production code; it's just too hard to control. I find it almost suitable for beginner programs - except that it causes a lot of confusion. Overall, the best advice is "don't be afraid scanf()

and fscanf()

". Note that this does not mean that you need to stay out of the way sscanf()

, although some care must be taken even when using sscanf()

.

+12


source


The return value scanf

indicates the number of elements successfully read and assigned to your variables, or EOF:

These functions successfully return the number of items in the input matched and assigned, which may be less than expected, or even zero in the early match event.

EOF is returned if the end of input is reached before the first successful conversion or corresponding failure. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror (3)) and errno is set to error.



If the int

return scanf

value is less than the number of input specifiers (or at least specifiers that need to be passed as an argument) in the format string, you can assume that something went wrong.

However, if you want to perform a more thorough check of a floating point number, you should consider using strtod()

. i.e. for input "1.23abc"

, scanf

saves it 1.23

to your variable and tells you that everything went well, but with the help strtod

you can check if the last converted character is actually the end of the string being processed.

+5


source







All Articles