C语言 使用 `scanf()` 读取逗号分隔的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15091284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:26:57  来源:igfitidea点击:

read comma-separated input with `scanf()`

ccsvinputfile-ioscanf

提问by Moonlit

I have the following input:

我有以下输入:

AG23,VU,Blablublablu,8
IE22,VU,FooBlaFooBlaFoo,3
and so on...

I want it to "parse" with scanf()using some code like this:

我希望它scanf()使用这样的代码来“解析” :

char sem[5];
char type[5];
char title[80];
int value;

while(scanf("%s,%s,%s,%d", sem, type, title, &value) == 4) {
 //do something with the read line values
}

But the execution of the code gives me: illegal instruction

但是代码的执行给了我: illegal instruction

How would you read a comma-separated file like this?

您将如何读取这样的逗号分隔文件?

回答by hmjd

The comma is not considered a whitespace character so the format specifier "%s"will consume the ,and everything else on the line writing beyond the bounds of the array semcausing undefined behaviour. To correct this you need to use a scanset:

逗号不被视为空白字符,因此格式说明符"%s"将消耗,和 行上的所有其他内容,写入超出数组边界,sem导致未定义行为。要纠正此问题,您需要使用扫描集:

while (scanf("%4[^,],%4[^,],%79[^,],%d", sem, type, title, &value) == 4)

where:

在哪里:

  • %4[^,]means read at most four characters or until a comma is encountered.
  • %4[^,]表示最多读取四个字符或直到遇到逗号。

Specifying the width prevents buffer overrun.

指定宽度可防止缓冲区溢出。

回答by Andrés AG

The problem that you are having is because when you say

你遇到的问题是因为当你说

 scanf("%s,%s,%s,%d", sem, type, title, &value) 

what happens is that you are trying doing is that you are fitting all the line into the first string which is just 5 characters. Therefore the sem[5]overflows, and soes all sorts of funny things. To avoid this problem, I tried using the expression %[^,], but it is not quite working. The best bet is to use something like

发生的情况是,您正在尝试将所有行放入第一个只有 5 个字符的字符串中。因此,sem[5]溢出,soes各种有趣的事情。为了避免这个问题,我尝试使用表达式%[^,],但它不太有效。最好的办法是使用类似的东西

while(scanf("%s%c%s%c%s%c%d", sem, &ch, type, &ch, title, &ch, &value) != EOF)

Then you can just discard the ch. However bear in mind that is better to use other functions to reading input such as getchar(), and things like that, which are much faster and safer in some sense.

然后你可以丢弃ch. 但是请记住,最好使用其他函数来读取输入,例如getchar(),以及类似的东西,它们在某种意义上更快更安全。