Questions tagged [istream]

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

696 questions
12
votes
2 answers

Why is there no std::from_string()?

Why is there no template T std::from_string(const std::string& s); in the C++ standard? (Seeing how there's an std::to_string() function, I mean.) PS - If you have an idea for the reason this was not adopted/considered, just…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
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 Zhlobo
  • 369
  • 2
  • 10
11
votes
2 answers

Can `std::istream::operator>>()` accept integer radix prefixes like stdio's %i format specifier?

When using scanf() and its variants, the format specifier %i will accept data as hex (prefixed "0x"), octal (prefixed "0"), or decimal (no prefix), so for example the strings "0x10", "020", and "16" are all converted to an integer with decimal value…
Clifford
  • 88,407
  • 13
  • 85
  • 165
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
  • 994
  • 8
  • 24
11
votes
2 answers

Shouldn't istream::peek() always return what you just putback()?

Intuitively, judging from the C++ spec, it looks to me as if istream::putback( c ) should always arrange the input buffer such that the next call to istream::peek() should read the character c. Is this not correct? I ask because the latest version…
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
10
votes
2 answers

How to use istream with strings

I would like to read an file into a string. I am looking for different ways for how to do it efficiently. Using a fixed size *char buffer I have received an answer from Tony what creates a 16 kb buffer and reads into that buffer and appends the…
hyperknot
  • 13,454
  • 24
  • 98
  • 153
10
votes
3 answers

C++ std::istream readsome doesn't read anything

It's like readsome isn't even reading. Returns 0 and doesn't read any chars. What is wrong here? #include #include int main () { std::fstream stream("list.cpp", std::ios::in); if (stream.good() || !stream.bad() ||…
Samuel Danielson
  • 5,231
  • 3
  • 35
  • 37
10
votes
1 answer

How to store formatting settings with an IOStream?

When creating formatted output for a user defined type it is often desirable to define custom formatting flags. For example, it would be nice if a custom string class could optionally add quotes around the string: String str("example"); std::cout <<…
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
10
votes
1 answer

C# IStream implemenation of IStream

First of all this is no duplicate of this Does a wrapper class for a COM interop IStream already exist? because I need the implemenation in the other direction. I need to create an IStream implemenation from IO.Stream to IStream. But before I start…
Florian
  • 5,918
  • 3
  • 47
  • 86
9
votes
2 answers

How can I find out how many bytes are available from a std::istream?

If I wanted to read() the content of a std::istream in to a buffer, I would have to find out how much data was available first to know how big to make the buffer. And to get the number of available bytes from an istream, I am currently doing…
edam
  • 718
  • 2
  • 8
  • 13
9
votes
1 answer

Why does operator>> on complex not set eofbit if it reaches EOF?

I'm trying to read as many std::complex as possible from a file (or any std::istream). If the operation fails, I check for ios::eof(). If it hasn't been set, I assume that there was an error in parsing the data, and I can report to the user…
dennis
  • 192
  • 7
9
votes
1 answer

What Effect Would LWG2349 Have?

While libstdc++ does not, libc++ does follow the standard which states that passing ios_base::failbit to basic_istream::exceptions has no effect on formatted input. For example this code: istringstream is{"ASD"}; double…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
9
votes
1 answer

boost::lexical_cast not recognizing overloaded istream operator

I have the following code: #include #include struct vec2_t { float x; float y; }; std::istream& operator>>(std::istream& istream, vec2_t& v) { istream >> v.x >> v.y; return istream; } int…
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
9
votes
2 answers

how can I convert istream * to string or just print it?

The below function part of connector/C++, it returns a istream*. if i just try and print it, it shows hex or a memory location because its a * type. istream *stream = res->getBlob(1); I tried to read & print it with this: string s; while…
user1397417
  • 708
  • 4
  • 11
  • 34
8
votes
2 answers

Conveniently copy std::vector to input stream (std::istream) object

I'm trying to make use of a function which comes in a 3rd party lib and expects an input stream object in which binary file data is transported. Signature looks like that: doSomething(const std::string& ..., const std::string& ..., …
Rip-Off
  • 358
  • 1
  • 4
  • 14
1
2
3
46 47