[英]Issues ignoring spaces with scanf when using multiple scanf's in C
I'm trying to use scanf
multiple times in a small program to grab inputs that are guaranteed to have spaces. 我试图在一个小程序中多次使用
scanf
来获取保证有空格的输入。 From the multiple threads I've browsed though it seems like scanf("%[^\\n]", string);
尽管看起来像
scanf("%[^\\n]", string);
但是从多个线程中浏览scanf("%[^\\n]", string);
is the way to get it to ignore spaces. 是使它忽略空格的方法。 This works, for one line, but any other scanf's after that line don't go through and their respective strings put out the following:
这适用于一行,但是该行之后的任何其他scanf都不会通过,并且它们各自的字符串如下:
Action: J���J
Resolution: J:F�J�B�J
Here is a bit of example code that I thought would work, but does not. 这里有一些示例代码,我认为可以,但是没有用。
#include <stdio.h>
int main(void)
{
char str1[100];
char str2[100];
printf("Situation?\n");
scanf("%[^\n]", str1);
printf("Action Taken?\n");
scanf("%[^\n]", str2);
printf("Situation: %s\n",str1);
printf("Action: %s\n",str2);
}
If I input "Just a test" when prompted for the situation the following happens: 如果在出现提示时输入“ Just a test”,则会发生以下情况:
Situation?
just a test
Action Taken?
Situation: just a test
Action: ��_��?�J.N=��J�J�d�����J0d���8d��TJ�J
Any suggestions or solutions (excluding fgets
)? 有什么建议或解决方案(不包括
fgets
)? An explanation of what's happening would be great as well. 对正在发生的事情的解释也很好。
Edit: The solution over at scanf: "%[^\\n]" skips the 2nd input but " %[^\\n]" does not. 编辑:在scanf处的解决方案:“%[^ \\ n]”跳过第二个输入,但“%[^ \\ n]”没有。 why?
为什么?
Adding in the char* fmt = "%[^\\n]%*c";
加入
char* fmt = "%[^\\n]%*c";
worked 100%. 工作了100%。
char* fmt = "%[^\n]%*c";
printf ("\nEnter str1: ");
scanf (fmt, str1);
printf ("\nstr1 = %s", str1);
printf ("\nEnter str2: ");
scanf (fmt, str2);
printf ("\nstr2 = %s", str2);
printf ("\nEnter str3: ");
scanf (fmt, str3);
printf ("\nstr2 = %s", str3);
printf ("\n");
change 更改
scanf("%[^\n]", str1);
to 至
scanf("%[^\n]%*c", str1);//consume a newline at the end of the line
Number of approaches: 方法数量:
Rather than the following which does not consume the Enter or '\\n'
(which is the problem): 而不是以下不消耗Enter或
'\\n'
的问题(这是问题所在):
scanf("%[^\n]",str1);
Consume the trailing newline. 消耗尾随的换行符。
"%*1[\\n]"
will only consume 1 '\\n'
, but not save it. "%*1[\\n]"
仅消耗1 '\\n'
,但不保存。
scanf("%99[^\\n]%*1[\\n]" ,str1);
Consume the trailing newline on the next scanf()
. 在下一个
scanf()
上使用结尾的换行符。 The " "
consume previous and leading white-space. " "
会占用先前的空白和前导空白。
scanf(" %99[^\\n]", str1);
Use fgets()
, but of course, this is not scanf()
. 使用
fgets()
,但是当然不是scanf()
。 The best method. 最好的方法。
fgets(str1, sizeof str1, stdin);
Whatever solution, limit the maximum charcters read and check the function's return value. 无论采用哪种解决方案,都应限制读取的最大字符数并检查函数的返回值。
if (fgets(str1, sizeof str1, stdin) == NULL) Handle_EOForIOError();
Solution one: Using scanf
解决方案一:使用
scanf
If you still want to read it by scanf
, the answer provided by @chux and @BLUEPLXY is good enough. 如果您仍然想用
scanf
读取它,@chux和@BLUEPLXY提供的答案就足够了。 Like: 喜欢:
scanf(" %[^\n]", str); //notice a space is in the formatted string
or 要么
scanf("%[^\n]%*c", str);
Solution two: Using getline()
(although it is a POSIX extension) 解决方案二:使用
getline()
(尽管它是POSIX扩展名)
Because using gets()
and 'fgets()` are unreliable sometimes. 因为有时使用
gets()
和'fgets()`是不可靠的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.