C语言 使用 scanf 读取由 / 分隔的字符串和整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12909840/
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
using scanf to read a string and an int separated by /
提问by Alcott
The input consists a string and an integer, which are separated by a '/', like this:
输入由一个字符串和一个整数组成,用 a 分隔'/',如下所示:
hello/17
And I want to read the input into a string and an int, like this:
我想将输入读入一个字符串和一个int,如下所示:
char str[20];
int num;
scanf("%s/%d", str, &num); // this how I tried to do it.
I can't seem to make it, any advice?
我似乎无法做到,有什么建议吗?
回答by StoryTeller - Unslander Monica
回答by paxdiablo
You only need to run the following program:
您只需要运行以下程序:
#include <stdio.h>
int main (void) {
char str[20] = {'String was 'hello/17'
Number was 42
Count was 1
'};
int count, num = 42;
count = sscanf ("hello/17", "%s/%d", str, &num);
printf ("String was '%s'\n", str);
printf ("Number was %d\n", num);
printf ("Count was %d\n", count);
return 0;
}
to see why this is happening. The output is:
看看为什么会这样。输出是:
#include <stdio.h>
int main (void) {
char str[20] = {'String was 'hello'
Number was 17
Count was 2
'};
int count, num = 42;
count = sscanf ("hello/17", "%[^/]/%d", str, &num);
printf ("String was '%s'\n", str);
printf ("Number was %d\n", num);
printf ("Count was %d\n", count);
return 0;
}
The reason has to do with the %sformat specifier. From C99 7.19.6.2 The fscanf function(largely unchanged in C11, and the italics are mine):
原因与%s格式说明符有关。来自 C99 7.19.6.2 The fscanf function(在 C11 中基本没有变化,斜体是我的):
s: matches a sequence of non-white-spacecharacters.
s: 匹配一系列非空白字符。
Since /is not white space, it gets included in the string bit, as does the 17for the same reason. That's also indicated by the fact that sscanfreturns 1, meaning that only oneitem was scanned.
由于/不是空白,它被包含在字符串位中,17出于同样的原因。这也由sscanf返回的事实表明1,这意味着只扫描了一项。
What you'll then be looking for is something that scans any characters otherthan /into the string (including white space). The same section of the standard helps out there as well:
什么,你会再寻找的东西,它可以扫描任何字符其他比/(包括空格)转换成字符串。标准的同一部分也有帮助:
[: matches a nonempty sequence of characters from a set of expected characters (the scanset). The conversion specifier includes all subsequent characters in the format string, up to and including the matching right bracket (]). The characters between the brackets (the scanlist) compose the scanset, unless the character after the left bracket is a circumflex (^), in which case the scanset contains all characters that do not appear in the scanlist between the circumflex and the right bracket.
[: 匹配一组预期字符(扫描集)中的非空字符序列。转换说明符包括格式字符串中的所有后续字符,直到并包括匹配的右括号 (])。括号之间的字符(扫描列表)组成扫描集,除非左括号后面的字符是一个抑扬符 (^),在这种情况下,扫描集包含所有未出现在抑扬符和右括号之间的扫描列表中的字符。
In other words, something like:
换句话说,类似于:
char str[20];
int num;
scanf("%19[^/]%*c%d", str, &num);
which gives you:
这给了你:
##代码##One other piece of advice: never everuse scanfwith an unbounded %sor %[; you're asking for a buffer overflow attack. If you want a robust user input function, see this answer.
另外一个忠告:从来没有过使用scanf与无界%s或%[; 您要求进行缓冲区溢出攻击。如果您想要强大的用户输入功能,请参阅此答案。
Once you have it in as a string, you can sscanfit to your heart's content without worrying about buffer overflow (since you've limited the size on input).
一旦将它作为字符串输入,您就可以sscanf随心所欲地使用它,而不必担心缓冲区溢出(因为您已经限制了输入的大小)。
回答by paxdiablo
Could be like that:
可能是这样的:
##代码##%*creads one character and discards it
%*c读取一个字符并丢弃它

