C语言 通过scanf()输入多字串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3555108/
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 06:17:14  来源:igfitidea点击:

multiple word string input through scanf( )

c

提问by prithu

what was the syntax to input strings with more than one word i.e with space in between through scanf() not gets()

输入包含多个单词的字符串的语法是什么,即通过 scanf() 而不是 gets()

回答by Chubsdad

Is it

是吗

scanf("%[^\t\n]",string);

回答by MrDhudy

char name[50];
printf("Enter your full name: ");
scanf("%[^\n]s",name);

Here [^\nindicates that scanf( )will keep receiving characters into name[ ]until a \nis encountered.

here[^\n表示scanf( )将继续接收字符 intoname[ ]直到\n遇到 a 。

回答by fat-lobyte

I don't think this is possible with scanf(). If you know the number of words you want to read, you can read it with

我认为 scanf() 不可能做到这一点。如果你知道你想阅读的字数,你可以用

char str1[100], str2[100];
scanf("%s %s", str1, str2);

Note that this is a huge security loophole, since a user can easily enter a string that's longer than the allocated space.

请注意,这是一个巨大的安全漏洞,因为用户可以轻松输入比分配空间更长的字符串。

If you don't know the number of words, you might have to rephrase your question. What do you need to read it for? Why don't you want to use gets(), why does it have to be scanf()?

如果您不知道字数,则可能需要重新表述您的问题。你需要阅读它做什么?为什么不想用gets(),为什么非得是scanf()?

回答by chux - Reinstate Monica

Better to used fgets()than scanf()for reading a line of user input.

使用fgets()scanf()读取一行用户输入更好。

If code must use scanf()then

如果代码必须使用scanf()那么

char buf[100];

// Read up to 99 char and then 1 \n
int count = scanf("%99[^\n]%*1[\n]", buf);

if (count == EOF) {
  Handle_EndOfFile();  // or IO error
}

if (count == 0) {  // Input began with \n, so read and toss it
  scanf("%*c");
}

Now parse buffor individual words.

现在解析buf单个单词。

回答by Leslie Satenstein

 char field1[40];
 char field2[40];
 char field3[40];
 char field4[40];
 char field5[40];
 char field6[40];
/*
 *  sscanf( workarea, format, field of pointers )   
 * Interpret  [^ ]      as a field ending in a blank
 * Interpret  [^' ']    as a field ending in a blank
 * Interpret [^ |\t]    as a field ending in blank or tab char
 * Interpret [^' '|\t]  as a field ending in blank or tab char
 * Interpret [^ |\t\n]  as a field ending in blank, tabchar or end-of-line
 *
 */
 strcpy(workarea,"Bread milk eggs cheese tomatoes cookies \n");
 i=sscanf(workarea," %[^' '|\t] %[^[' '|\t] %[^' '|\t]  %[^' '|\t] %[^' '|\t] %[^' '|\t|\n] ",
             field1,field2,field3,field4,field5,field6);

This scan results in field1 containing "Bread", field2 containing "milk",... field6 containing "cookies". Between the first to last words you may one or more blanks or tabs The ending following cookies may be one of the three of space, tab or newline which will be dropped and not be part of "cookies".

此扫描导致字段 1 包含“面包”,字段 2 包含“牛奶”,...字段 6 包含“饼干”。在第一个到最后一个单词之间,您可以有一个或多个空格或制表符 结尾的 cookie 可能是空格、制表符或换行符这三者中的一个,它们将被删除,而不是“cookies”的一部分。

回答by ecruz

You could read an entire line from a file if you want with:

如果需要,您可以从文件中读取整行:

scanf("%[^\n]\n", line);

Now, you could use sscanf to get every word:

现在,您可以使用 sscanf 来获取每个单词:

sscanf(line, "%s", word);
line += strlen(word) + 1;

"line" and "word" are char pointers.

“line”和“word”是字符指针。

Note how line is going towards to get to the next word.

注意 line 是如何到达下一个单词的。