In C++ std::istream is the base class for input streams.
Questions tagged [istream]
696 questions
145
votes
5 answers
Why does std::getline() skip input after a formatted extraction?
I have the following piece of code that prompts the user for their cat's age and name:
#include
#include
int main()
{
int age;
std::string name;
std::cin >> age;
std::getline(std::cin, name);
if…

David G
- 94,763
- 41
- 167
- 253
98
votes
6 answers
Using C++ filestreams (fstream), how can you determine the size of a file?
I'm sure I've just missed this in the manual, but how do you determine the size of a file (in bytes) using C++'s istream class from the fstream header?

warren
- 32,620
- 21
- 85
- 124
60
votes
7 answers
Get an istream from a char*
I have a char* and the data length that I'm receiving from a library, and I need to pass the data to a function that takes an istream.
I know I can create a stringstream but that will copy all the data. And also, the data will surely have 0s since…

Damian
- 5,471
- 11
- 56
- 89
57
votes
2 answers
C++ streams confusion: istreambuf_iterator vs istream_iterator?
What is the difference between istreambuf_iterator and istream_iterator?
And in general what is the difference between streams and streambufs?
I really can't find any clear explanation for this so decided to ask here.

dextrey
- 815
- 7
- 9
46
votes
5 answers
Why is istream/ostream slow
At 50:40 of http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly Andrei Alexandrescu makes a joke about how not efficient/slow istream is.
I had an issue in the past with ostream being slow and fwrite being…
user34537
28
votes
5 answers
Reading binary istream byte by byte
I was attempting to read a binary file byte by byte using an ifstream. I've used istream methods like get() before to read entire chunks of a binary file at once without a problem. But my current task lends itself to going byte by byte and relying…

Adrian McCarthy
- 45,555
- 16
- 123
- 175
26
votes
2 answers
Why istream object can be used as a bool expression?
Does anyone know why istream object can be used as bool expression? For example:
ifstream input("tmp");
int iValue;
while (input >> iValue)
//do something;
Here input >> iValue returns a reference to the ifstream object. I want to know why this…

cheng
- 2,106
- 6
- 28
- 36
23
votes
5 answers
istream::getline return type
What does the istream::getline method return?
I am asking because I have seen that to loop through a file, it should be done like this:
while ( file.getline( char*, int ) )
{
// handle input
}
What is being returned?
user542687
17
votes
5 answers
non-copying istringstream
So istringstream copies the contents of a string when initialised, e.g
string moo("one two three four");
istringstream iss(moo.c_str());
I was wondering if there's a way to make std::istringstream use the given c_str as its buffer without copying…

kamziro
- 7,882
- 9
- 55
- 78
15
votes
2 answers
How to detect empty lines while reading from istream object in C++?
How can I detect if a line is empty?
I have:
1
2
3
4
5
I'm reading this with istream r
so:
int n;
r >> n
I want to know when I reach the space between 4 and 5.
I tried reading as char and using .peek() to detect \n but this detects the \n that…

bb2
- 2,872
- 7
- 37
- 45
15
votes
2 answers
Input from an istream to a char* pointer?
I am reading Bjarne Stroustrup's "Programming Principles and Practice Using C++" (second edition). On page 660-661, the writers define a function as follows:
istream& read_word(istream& is, char* buffer, int max)
// read at most max-1 characters…

CPPL
- 726
- 1
- 10
15
votes
3 answers
non-blocking std::getline, exit if no input
Currently I have a program that reads from the standard input, occasionally the program needs to just keep running if no input is made, usually this is a test script there is no 'enter' so to speak.
program -v1 -v2 -v3 output
v1 - v3 are…

SGE
- 2,317
- 3
- 19
- 16
13
votes
6 answers
How to assign istringstream and ifstream to an istream variable?
I want to have a variable of type istream which can hold either the contents of a file or a string. The idea is that if no file was specified, the variable of type istream would be assigned with a string.
std::ifstream…

oddRaven
- 672
- 1
- 7
- 20
13
votes
3 answers
Skip lines in std::istream
I'm using std::getline() to read lines from an std::istream-derived class, how can I move forward a few lines?
Do I have to just read and discard them?

ufk
- 30,912
- 70
- 235
- 386
12
votes
1 answer
Distinguishing between failure and end of file in read loop
The idiomatic loop to read from an istream is
while (thestream >> value)
{
// do something with value
}
Now this loop has one problem: It will not distinguish if the loop terminated due to end of file, or due to an error. For example, take the…

celtschk
- 19,311
- 3
- 39
- 64