In C++ std::istream is the base class for input streams.
Questions tagged [istream]
696 questions
5
votes
2 answers
How to block on reading a c++ stringstream to wait for data
So, I've been trying to figure out, how to wait for data from a C++ stringstream (for instance), without being constantly checking if data is there, which is quite CPU consuming.
I'm perfectly able to read, for instance, from a serial device, and…

cvicente
- 132
- 3
- 12
5
votes
2 answers
Is this a bug with getline(), or am I doing something wrong. Right way to use getline()?
It might not be a bug, but I don't know what is going wrong.
My first entry is repeated for str1 on 2nd iteration, and is same way from then. Only first iteration goes good.
#include
#include
using namespace std;
int main ()…

ani
- 109
- 2
- 11
4
votes
1 answer
Is it guaranteed that std::char_traits::to_int_type(c) == static_cast(c)?
The question How to use correctly the return value from std::cin.get() and std::cin.peek()? made me wonder if it is guaranteed that
std::char_traits::to_int_type(c) == static_cast(c)
for all valid char values c.
This comes up in a lot…

L. F.
- 19,445
- 8
- 48
- 82
4
votes
1 answer
What is an istream_view and when do I use one?
Apparently, C++20 has a new std::istream-related construct: std::istream_view. The cppreference page on it is a stub right now†. So, what is a "view of an istream" and what can I use it for?
† - Ok, technically it redirects to a page about…

einpoklum
- 118,144
- 57
- 340
- 684
4
votes
1 answer
why does a blank line occupy to two bytes?
I'm new to C++. I'd like to count the blank lines at the end of a text file. But now i meet a problem. The contents of the text file are like:
test.txt
1
2
blank line
The code is:
#include
#include
using namespace std;
int…

Oliver Yu
- 99
- 5
4
votes
1 answer
Is it always safe to use std::istream::peek()?
I usually teach my students that the safe way to tackle file input is:
while (true) {
// Try to read
if (/* failure check */) {
break;
}
// Use what you read
}
This saved me and many people from the classical and most of the…

Costantino Grana
- 3,132
- 1
- 15
- 35
4
votes
2 answers
Handling invalid input in input stream operator >>
What is the recommended (standard) way of handling invalid input in extraction operator:
std::istream& operator>>(std::istream& is, SomeType& val) {
// ...
return is;
}
Should it set std::ios_base::failbit and return immediately? Is it okay…

trozen
- 1,117
- 13
- 13
4
votes
2 answers
insert a string in front of a istream in cpp
My problem is something like I want to append some string in front of a iostream. You can say in front of std::cin.
#include
#include
void print(std::istream & in){// function not to be modified
std::string str;
in >>…

keen_learner
- 211
- 4
- 8
4
votes
1 answer
istream's tellg/seekg cannot be protected from stack smashing (g++)?
For a program that I'm writing, it is useful for me to calculate file sizes, which I calculate by using iostream's tellg and seekg functions, but this leads to a warning by -Wstack-protector. The following code reproduces the "problem":
#include…

Zorawar
- 6,505
- 2
- 23
- 41
4
votes
1 answer
.NET equivalent of Delphi IStream initialization
I have the following Delphi code:
var
Stream: TMemoryStream;
StreamI: TStreamAdapter;
OleStream: IStream;
begin
Stream:= TMemoryStream.Create;
Stream.LoadFromFile(filename);
StreamI:= TStreamAdapter.Create(Stream, soOwned);
…

SharpAffair
- 5,558
- 13
- 78
- 158
4
votes
4 answers
C++ common interface for "cin" and "File"
Is there a common interface for cin and file input?
I want to make a program that has an optional parameter
prog [input-file]
If an input file is specified, then it should read from the file, and if not, it should read from cin.
From what I can…

Verhogen
- 27,221
- 34
- 90
- 109
4
votes
2 answers
Why `s.clear(ios::badbit);` below? Why not `s.clear(ios::failbit);`?
I was looking into this question and I have no problem understanding the two answers given to it. But I'm not sure I understood the s.clear(ios::badbit); in the statement highlighted below with the comment // set state. For instance, why not…

John Kalane
- 1,163
- 8
- 17
4
votes
1 answer
Reading in one byte at a time with .get()
So i'm reading in a input file that contains:
lololololololol
I need to read it in using binary one byte at a time for something I'm doing later on. To do this i'm using get() to read it in then storing it into a char. It seems to be working…

David Dennis
- 702
- 2
- 9
- 26
4
votes
1 answer
istream with popen functionality
I have a function that accepts istream class.
I need to be able to make it work with gzip data.
Is there in C++ standard istream-like class with popen()-like functionality?
Alternatively, is there a way to convert FILE * to istream?

Nick
- 9,962
- 4
- 42
- 80
4
votes
1 answer
Using cin for keyboard input after processing redirected file input with getline
I know, this question has been treated tons of times.. but I can't make it work anyway.. here I paste some code:
#include
#include "header.h"
using namespace std;
using namespace header;
int main(){
string parent, child, line, node;
…

Moriduri
- 107
- 1
- 10