【发布时间】:2016-02-06 18:46:48
【问题描述】:
我想知道从命令行获取输入的可接受方式是什么,它也捕获空白。我以为这样就可以了...
char text[500];
int textSize = 0;
int main() {
while (!cin.eof()) {
cin >> text[textSize];
textSize++;
}
for(int i = 0; i < textSize; i++) {
cout << text[i];
}
return 0;
}
但看起来它跳过了空白。我切换到这个...
char c;
while ((c = getchar()) != EOF) {
text[textSize] = c;
textSize++;
}
效果很好,但我从一本 C 编程书中知道这一点。想知道我将如何在 C++ 中处理它
【问题讨论】:
-
你可以使用
istream::get -
char text[500];而是使用std::string。 -
while (!cin.eof())是个错误 -
我会看看 istream。谢谢! MM,你说的错误是什么意思?