Questions tagged [istream]

In C++ std::istream is the base class for input streams.

658 questions
121
votes
3 answers

Why does std::getline() skip input after a formatted extraction?

I have the following piece of code that prompts the user for their name and state: #include #include int main() { std::string name; std::string state; if (std::cin >> name && std::getline(std::cin, state)) { …
0x499602D2
  • 87,005
  • 36
  • 149
  • 233
81
votes
5 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
  • 28,486
  • 19
  • 80
  • 115
58
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,291
  • 9
  • 52
  • 86
50
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
  • 745
  • 6
  • 9
39
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
27
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
  • 41,073
  • 12
  • 108
  • 157
25
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
  • 1,936
  • 5
  • 25
  • 34
20
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
13
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,452
  • 7
  • 30
  • 44
13
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,087
  • 3
  • 16
  • 16
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
  • 18,046
  • 2
  • 34
  • 61
12
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,112
  • 7
  • 51
  • 74
11
votes
3 answers

Input from stream to enum type

How to input from stream to enum type? I can do it so unsigned int sex = 0; stream >> sex; student.m_bio.sex = static_cast(sex); Otherwise?
Dmitry
  • 239
  • 2
  • 10
11
votes
1 answer

In C++, calling fork when cin is a bash heredoc causes repeated input fragments

I am implementing a shell-like program in C++. It has a loop that reads from cin, forks, and waits for the child. This works fine if the input is interactive or if it's piped from another program. However, when the input is a bash heredoc, the…
Kevin Chen
  • 964
  • 8
  • 24
11
votes
4 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
  • 26,596
  • 55
  • 202
  • 346
1
2 3
43 44