Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
用户键入一个字符串,可能由制表符、空格和“输入”(CR) 分隔。我需要全部接收;问题是当用户按下“Enter”键时,gets() 函数会停止扫描。还有另一种方法吗?除了scanf和gets之外,我不能使用任何其他功能。
首先:gets()是真的邪恶。永远不要使用它。
gets()
使用 .编写正确的程序是不可能的gets()。
考虑read(), getchar(), ...
read()
getchar()
使用循环。继续getcing(将结果放入足够大的缓冲区中),直到遇到EOF.
getc
EOF
为什么需要“输入”?您可以通过返回的事实知道检测到回车符(或文件结尾)。