#include <bits/stdc++.h>
using namespace std;
char s[1005];
int main() {
while(true) {
scanf("%[^\n]%*c", s);
printf("%s\n", s);
}
return 0;
}
以上代码模拟了 std::getline 的功能。scanf 读入字符串 s,并且在读到 \n 时停止。最后的 %*c 把换行符读入。
输入任何正常的字符串均无问题,但是当你输入空串时会导致换行符无法读入且 s 中也没有 \0。
我怀疑这是 scanf 的 bug。
平台:Windows 7
编译器版本:gcc 11.2.0