I am having trouble with multiple chars and scanf_

2019-02-27 23:48发布

问题:

I'm trying to use scanf_s() to read in multiple values but every time I run the program, I get

Unhandled exception at 0x592AD6AC (msvcr120d.dll) in lab 2.exe: 0xC0000005: Access violation writing location 0x00000000.

in a popup window. How do I fix this?

float inTemp;
char inUnit;
char outUnit;

printf("Please enter the starting temperature with its units and the units\nyou would like to convert to (i.e. 74.5 F C): ");
scanf_s("%f %c %c", &inTemp, &inUnit, &outUnit); //takes in all user input (NOT WORKING)

回答1:

You have not provided a length indicator. From MSDN:

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if you are reading a string, the buffer size for that string is passed as follows: char s[10]; scanf_s("%9s", s, _countof(s)); // buffer size is 10, width specification is 9

Here is a link to that page.



回答2:

If you really want to use (less portable) scanf_s, from the C11 standard (n1570), Annex K 3.5.3.2 p.4:

The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a *). The first of these arguments is the same as for fscanf. That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair.

You need to give the lengths of your char * arguments:

scanf_s("%f %c %c", &inTemp, &inUnit, 1, &outUnit, 1);

Alternatively, just use scanf:

scanf("%f %c %c", &inTemp, &inUnit, &outUnit);

And, as always, check the return values.

In general, as scanf is somtimes useful for little quick-and-dirty programmes, it’s of less use for productive software, because handling errors is hard. Use fgets and sscanf/strtod/strtol/… instead.