In C++ std::istream is the base class for input streams.
Questions tagged [istream]
696 questions
7
votes
4 answers
Read from cin or a file
When I try to compile the code
istream in;
if (argc==1)
in=cin;
else
{
ifstream ifn(argv[1]);
in=ifn;
}
gcc fails, complaining that operator= is private. Is there any way to set an istream to different values based on a…

m42a
- 1,322
- 2
- 10
- 14
7
votes
2 answers
How to check if there is anything in cin [C++]
is there any way to check if there is something in cin? I tryied peek() but if there isn't anything peek() waits for input and that isn't what I want.
Thank you

There is nothing we can do
- 23,727
- 30
- 106
- 194
7
votes
1 answer
Concurrency problems wih Boost Property tree
I developed a simple wrapper that encapsulates a JSONObject with Boost Property trees.
The problem is a segmentation fault in this code:
void JSONObject::parse(const std::string &text)
{
std::istringstream ss(text);
…

mariolpantunes
- 1,114
- 2
- 15
- 28
7
votes
1 answer
Is this a compiler bug or it's my code?
Here is a sample code:
#include
#include
#include
#include
#include
using std::cout;
using std::endl;
std::size_t const BUF_SIZE(1000);
std::ostream& operator<<(std::ostream& os, std::tm const&…

Reza Toghraee
- 1,603
- 1
- 14
- 21
6
votes
6 answers
Parsing only numbers from istream in C++
I have a bunch of input files that look like the following:
(8,7,15)
(0,0,1) (0,3,2) (0,6,3)
(1,0,4) (1,1,5)
I need to write a function that parses these inputs one number at a time, so I need to be able to separate the input by numbers, e.g.: 8,…

Arvin
- 1,391
- 4
- 19
- 33
6
votes
2 answers
How to mock method returning istream&?
I have mocked virtual method returning istream&. I'd like to use it in a testcase. How to return some value?
The problem is that istream is noncopyable.
I try something like this:
TEST(x, y)
{
MockClass mock;
std::istringstream str("Some…

peter55555
- 1,413
- 1
- 19
- 36
6
votes
1 answer
Calling putback() on istream multiple times
Many sites describe the istream::putback() function that lets you "put back" a character into the input stream so you can read it again in a subsequent reading operation.
What's to stop me, however, from calling putback() multiple times in sequence…

Izhido
- 392
- 3
- 13
6
votes
3 answers
How can you pass an std::istream into a function in a way that allows to pass temporaries?
I am trying to create a constructor to load a resource from any istream given to it. I cannot seem to figure out the best way to pass the istream parameter into a constructor.
Loader::Loader(istream stream);
This one is obviosly bad due to object…

Tobias
- 924
- 9
- 23
6
votes
5 answers
What's the difference between getline and std::istream::operator>>()?
#include
#include
using namespace std;
int main()
{
string username;
cout<< "username" ;
cin >> username;
}
So I was curious on what's the difference between these two codes, I heard it's the same thing but if it is…

user4857442
- 69
- 1
- 1
- 4
6
votes
2 answers
GCC 4.7 istream::tellg() returns -1 after reaching EOF
The following code works with gcc 4.4.
But gcc 4.7 will give assertion failure.
#include
#include
#include
using namespace std;
int main()
{
string input("abcdefg");
stringstream iss(input);
…

John Crane
- 371
- 5
- 14
5
votes
4 answers
istream extraction operator: how to detect parse failure?
How can I detect whether the istream extraction failed like this?
string s("x");
stringstream ss(s);
int i;
ss >> std::ios::hex >> i;
EDIT -- Though the question title covers this, I forgot to mention in the body: I really want to detect whether…

xtofl
- 40,723
- 12
- 105
- 192
5
votes
0 answers
C++ changin istream
I am learning C++ at the moment and am working to understand streams. Today I learned this really cool thing, you can split a string stream into multiple floats/integers, like:
#include
#include
using namespace std;
int main()…

Robin Dorstijn
- 101
- 5
5
votes
1 answer
How to use libjpeg to read a JPEG from a std::istream?
libjpeg can read JPEG data from a FILE* or a buffer. My data is coming from a std::istream. I could read the entire std::istream into a buffer to use with libjpeg, but I'd rather have libjpeg read directly from the std::istream if possible. How can…

uckelman
- 25,298
- 8
- 64
- 82
5
votes
1 answer
Can I use istream_iterator to copy some istream content into std::string?
I have an istream and need to copy the content between two delimiters to a std::string.
I can find the delimiters' streampos, but when trying to use istream_iterator to iterate over the section of the stream, it does not work. Here's what I…

RL-S
- 734
- 6
- 21
5
votes
0 answers
Read from stream Hex Float
I am trying to read a hex float value through std::cin. However, it just reads in 0. Here is my code so far:
#include
#include
int main() {
double f = 0.0;
std::cout << ">";
std::cin >> std::hexfloat >> f;
std::cout <<…

Arden Rasmussen
- 95
- 9