我可以用什么代替 scanf 进行输入转换? [英] What can I use for input conversion instead of scanf?
问题描述
我经常看到有人劝阻其他人不要使用 scanf
并说有更好的选择.但是,我最终看到的要么是不要使用 scanf
" 或 这是一个正确的格式字符串",而且从来没有任何示例提到的更好的选择".
I have very frequently seen people discouraging others from using scanf
and saying that there are better alternatives. However, all I end up seeing is either "don't use scanf
" or "here's a correct format string", and never any examples of the "better alternatives" mentioned.
例如,让我们看这段代码:
For example, let's take this snippet of code:
scanf("%c", &c);
这会读取最后一次转换后留在输入流中的空白.通常建议的解决方案是使用:
This reads the whitespace that was left in the input stream after the last conversion. The usual suggested solution to this is to use:
scanf(" %c", &c);
或者不使用scanf
.
既然scanf
不好,那么scanf
通常可以处理哪些转换输入格式的ANSI C 选项(例如整数、浮点数和字符串)不使用scanf
?
Since scanf
is bad, what are some ANSI C options for converting input formats that scanf
can usually handle (such as integers, floating-point numbers, and strings) without using scanf
?
推荐答案
最常见的读取输入的方式是:
The most common ways of reading input are:
使用具有固定大小的
fgets
,这是通常的建议,并且
using
fgets
with a fixed size, which is what is usually suggested, and
使用 fgetc
,如果您只读取单个 char
,这可能很有用.
using fgetc
, which may be useful if you're only reading a single char
.
要转换输入,您可以使用多种函数:
To convert the input, there are a variety of functions that you can use:
strtoll
,将字符串转换为整数
strtof
/d
/ld
,将字符串转换为浮点数
strtof
/d
/ld
, to convert a string into a floating-point number
sscanf
,虽然它确实有下面提到的大部分缺点
sscanf
, which is not as bad as simply using scanf
, although it does have most of the downfalls mentioned below
在普通 ANSI C 中没有解析分隔符分隔的输入的好方法.使用 POSIX 中的 strtok_r
或 strtok
,它不是线程-安全的.你也可以自己动手 使用 strcspn
和 strspn
的线程安全变体,因为 strtok_r
不涉及任何特殊的操作系统支持.
There are no good ways to parse a delimiter-separated input in plain ANSI C. Either use strtok_r
from POSIX or strtok
, which is not thread-safe. You could also roll your own thread-safe variant using strcspn
and strspn
, as strtok_r
doesn't involve any special OS support.
这可能有点矫枉过正,但您可以使用词法分析器和解析器(flex
和 bison
是最常见的例子).
It may be overkill, but you can use lexers and parsers (flex
and bison
being the most common examples).
无需转换,只需使用字符串
No conversion, simply just use the string
由于我没有详细说明为什么 scanf
在我的问题中不好,我将详细说明:
Since I didn't go into exactly why scanf
is bad in my question, I'll elaborate:
使用转换说明符
%[...]
和%c
,scanf
不会占用空格.这显然并不广为人知,正如 的许多重复所证明的那样这个问题.
With the conversion specifiers
%[...]
and%c
,scanf
does not eat up whitespace. This is apparently not widely known, as evidenced by the many duplicates of this question.
在引用 scanf
的参数(特别是字符串)时,对于何时使用一元 &
运算符存在一些混淆.
There is some confusion about when to use the unary &
operator when referring to scanf
's arguments (specifically with strings).
很容易忽略来自 scanf
的返回值.这很容易因读取未初始化的变量而导致未定义的行为.
It's very easy to ignore the return value from scanf
. This could easily cause undefined behavior from reading an uninitialized variable.
很容易忘记在scanf
中防止缓冲区溢出.scanf("%s", str)
和 gets
一样糟糕,甚至更糟.
It's very easy to forget to prevent buffer overflow in scanf
. scanf("%s", str)
is just as bad as, if not worse than, gets
.
使用 scanf
转换整数时,您无法检测溢出.实际上,溢出会导致未定义行为在这些函数中.
You cannot detect overflow when converting integers with scanf
. In fact, overflow causes undefined behavior in these functions.
这篇关于我可以用什么代替 scanf 进行输入转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!