如何从 std::cin 读取直到流结束?
我的问题是,我想从 std::cin
读取输入,但不知道输入有多长。另外,我必须char
并且不能使用std::string
。 我必须处理两种方法: a) 用户输入文本,当他点击 [ENTER] 时,程序停止读取。 b) 用户将 std::cin
重定向到一个可以容纳多行的文件(例如 .\a.oput < file
)。
编辑:刚刚注意到 std::cin.eof() 在读取文件的情况下也始终为 false。
对于 a) 我可以一直读到 \n 出现。对于 b) 编辑:否 (我可以阅读直到 std::cin.eof() 发生。) 但是,当我不知道自己遇到的是 a) 还是 ab) 问题时,如何中断阅读过程?
这就是我的 a) ...
char buffer = ' ';
while(std::cin >> std::noskipws >> buffer && buffer != '\n')
{
// do some stuff with buffer
}
... 和 b)
char buffer = ' ';
while(std::cin >> std::noskipws >> buffer)
{
// do some stuff with buffer
}
我还知道有 std::cin.tellg()
来获取流中的当前位置。
编辑:因此,在文件的情况下,输入流似乎被终止,以 std::cin >> 的方式终止。 std::noskipws>> buffer
得到 false
。 上面的代码对 a) 的作用:
- 它等待用户输入并按 [ENTER]
- 然后循环遍历用户在最后一行输入的每个字符。
- 然后它再次等待用户输入并按 [ENTER]
- 无限等待处理循环
那么我该怎么做呢?
My problem is, that I want to read the input from std::cin
but don't know how long the input is. Also I have to char
and can't use std::string
.
There are two ways I have to handle:
a) The user inputs text and when he hits [ENTER] the program stops reading.
b) The user redirects std::cin
to a file (like .\a.oput < file
) which can hold multiple lines.
Edit: Just noticed that std::cin.eof() is always false also in the case of reading form a file.
For a) I could read until \n occures. For b) Edit: No (I could read until std::cin.eof() occures.)
But when I don't know whether I'm getting an a) or a b) problem, how can I break the reading process?
This is what I have for a) ...
char buffer = ' ';
while(std::cin >> std::noskipws >> buffer && buffer != '\n')
{
// do some stuff with buffer
}
... and b)
char buffer = ' ';
while(std::cin >> std::noskipws >> buffer)
{
// do some stuff with buffer
}
Also I know there is std::cin.tellg()
to get the current position in the stream.
Edit: So it seems like in case of the file the input streams gets terminated, in the way that std::cin >> std::noskipws >> buffer
gets false
.
What the code above does for a):
- It waits for the user to make an input and press [ENTER]
- Then it loops through every char entered by the user on the last line.
- Then it waits again for the user to make an input and press [ENTER]
- Infinite-waiting-processing-loop
So how do I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以要求输入始终结束 EOF(这意味着从命令行需要按下
^D
),然后一如既往地使用 b 的过程。这也将启用来自 cmdline 的多行输入You could require the input to always end EOF (meaning from commmand line requiring
^D
to be pressed) and then use the process for b as always. This would then enable multiline input from cmdline as well您可以使用(旧) getline 函数,该函数需要一个指向char 数组,可以使用分隔符。但是您无法确保在每种情况下它都会读取到 eof (因为 char 缓冲区可能太小),但是使用 char 数组而不注意它的大小是非常危险的(并且从无论如何,安全点是灾难性的,因为它可能导致缓冲区溢出,很容易被利用。
此代码应该使您能够从输入中逐行提取:
这可以读取最大数量的字符:
You could use the (old) getline function, which takes a pointer to a char array and can use a delimiter. But you wont be able to ensure that in every case it will read to the eof (as the char buffer might be too small), but using a char-array and not paying attention to the size of it is a very dangerous (and from the point of security catastrophic) thing anyways as it can lead to buffer-overflows which can be easily exploited.
This code should enable you to extract line by line from the input:
This to read a maximum amount of chars: