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?