scanf("%*[\n] %[^\n]", input_string);做? [英] What does scanf("%*[\n] %[^\n]", input_string); do?

查看:55
本文介绍了scanf("%*[\n] %[^\n]", input_string);做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解其中的区别.我使用 %[^\n]s 来获取用户输入的短语.但是当我需要添加两个短语时,这不起作用.但是上面那个做到了.请帮助我理解其中的区别.

I am not able to understand the difference. I use %[^\n]s, for taking phrases input from the user. But this was not working when I needed to add two phrases. But the above one did. Please help me understanding me the difference.

推荐答案

%[\n] 指令告诉 scanf() 匹配换行符,而 * 标志表示不应进行赋值,因此 %*[\n] 跳过任何前导换行符(假设 至少有一个领先的 \n 字符:稍后会详细介绍).第一个指令后面有一个空格,因此在最后一个 %[^\n] 指令之前跳过零个或多个空白字符,该指令匹配字符直到遇到换行符.它们存储在 input_string[] 中,换行符留在输入流中.使用此格式字符串的后续调用将跳过此剩余的换行符.

The %[\n] directive tells scanf() to match newline characters, and the * flag signals that no assignment should be made, so %*[\n] skips over any leading newline characters (assuming there is at least one leading \n character: more on this in a moment). There is a space following this first directive, so zero or more whitespace characters are skipped before the final %[^\n] directive, which matches characters until a newline is encountered. These are stored in input_string[], and the newline character is left behind in the input stream. Subsequent calls using this format string will skip over this remaining newline character.

但是,这里可能不需要 %*[\n] 指令,因为 \n 是一个空白字符;使用格式字符串中的前导空格可以完成几乎相同的事情:" %[^\n]".

But, there is probably no need for the %*[\n] directive here, since \n is a whitespace character; almost the same thing could be accomplished with a leading space in the format string: " %[^\n]".

两者之间的一个区别:"%*[\n] %[^\n]" 期望在输入的开头有一个换行符,没有这个匹配失败并且 scanf() 返回而不进行任何分配,而 " %[^\n]" 不期望前导换行符,甚至前导空白字符(但如果存在则跳过它们).

One difference between the two: "%*[\n] %[^\n]" expects there to be a newline at the beginning of the input, and without this the match fails and scanf() returns without making any assignments, while " %[^\n]" does not expect a leading newline, or even a leading whitespace character (but skips them if present).

如果您使用 "%[^\n]" 代替,如问题正文中所建议的那样(注意尾随 s 不是scanset 指令),第一次调用 scanf() 将匹配字符,直到遇到换行符.匹配的字符将存储在 input_string[] 中,换行符将保留在输入流中.然后,如果使用此格式字符串再次调用 scanf() ,则在遇到换行符之前不会匹配任何字符,因此匹配将失败而不赋值.

If you used "%[^\n]" instead, as suggested in the body of the question (note that the trailing s is not a part of the scanset directive), the first call to scanf() would match characters until a newline is encountered. The matching characters would be stored in input_string[], and the newline would remain in the input stream. Then, if scanf() is called again with this format string, no characters would be matched before encountering the newline, so the match would fail without assignment.

请注意,在 scanf() 格式字符串中使用 %s%[] 时,应始终指定最大宽度以避免缓冲区溢出.无论使用 %s 还是 %[]scanf() 都会自动添加 \0 终止符,因此您必须确保为此留出空间.对于大小为 100 的数组,最大宽度应为 99,以便在添加空终止符之前最多匹配 99 个字符并存储在数组中.例如:" %99[^\n]".

Please note that you should always specify a maximum width when using %s or %[] in a scanf() format string to avoid buffer overflow. With either of %s or %[], scanf() automatically adds the \0 terminator, so you must be sure to allow room for this. For an array of size 100, the maximum width should be 99, so that at most 99 characters are matched and stored in the array before the null terminator is added. For example: " %99[^\n]".

这篇关于scanf("%*[\n] %[^\n]", input_string);做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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