我可以使用什么来进行输入转换而不是 scanf? [英] What can I use for input conversion instead of scanf?

查看:16
本文介绍了我可以使用什么来进行输入转换而不是 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.

以这段代码为例:

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 不好,有哪些 ANSI C 选项可用于转换 scanf 通常可以处理的输入格式(例如整数、浮点数和字符串)不使用 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安全的.你也可以自己动手 使用 strcspnstrspn 的线程安全变体,因为 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.

这可能有点矫枉过正,但您可以使用词法分析器和解析器(flexbison 是最常见的示例).

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:

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆