Questions tagged [iostream]

The C++ iostream library is an object-oriented library that provides input and output functionality using streams. The iostreams classes support type-safe I/O of built-in types and can be extended to support user-defined types by overloading the >> and << operators.

Use this tag for questions about using iostreams, including writing overloaded operators for your own types.

The C++ standard library defines the std::istream, std::ostream and std::iostream base classes, as well as the standard stream objects std::cin, std::cout and std::cerr and derived iostream types for reading/writing files and strings. An iostream object is responsible for formatting operations (such as converting an integer 1234 into the string "1234" or vice versa) but uses a stream buffer (an object derived from std::streambuf) to interface with the underlying data stream. The stream buffer does any buffering of characters, manages the stream position and transports characters to/from an external device such as a file.

Iostreams use locales to support internationalization and are implemented as a hierarchy of class templates to support different types of characters, so that std::istream is actually a typedef for the specialization std::basic_istream<char, std::char_traits<char>>. The first template parameter is the character type used by the stream and the second is a traits class that provides operations for working with the character type.

A quick introduction to iostreams can be found in Chapter 3: A tour of the Standard Library in Stroustrup's TC++PL. Josuttis's The C++ Standard Library gives more information. The most detailed reference on using and extending iostreams is Langer & Kreft's Standard C++ IOStreams and Locales (see excerpt about stream buffers.)

2485 questions
1
vote
1 answer

C++ Multiple writing points stream

I would like to find/implement a c++ (hopefully stl) compatible stream that supports multiple writing points. What I mean with multiple writing points is easy to explain with the following example. Lets say that you want to generate a source code…
gsf
  • 6,612
  • 7
  • 35
  • 64
1
vote
2 answers

Fill a buffer with iostream operator

I want to use << operator to fill an external buffer, I have tried to inherit from iostream, and then every time I use << to my class, I copy the content, into the current buffer location, and increment the buffer location... I had no luck, I looked…
aah134
  • 860
  • 12
  • 25
1
vote
1 answer

How to clear the state bits in an iostream object in C++?

I'm trying to learn C++ from an older edition of the Primer, and tried to execute some of their code relating to iostream objects, which gave me some trouble: #include #include using namespace std; int main(int argc, char…
1
vote
4 answers

C++ cin weird behaviour

I have the following code: int data = 0; cout << "Enter a number: "; cin >> data; cout << "You entered " << data << endl; string str; cout << "Enter a string: "; getline(cin,str); cout << "Your entered " << str <<…
user2465355
  • 375
  • 1
  • 3
  • 15
1
vote
4 answers

How to read in a set of values from a text file, then go to the next line and do the same

Im trying to read a list like this one James John 15 5 1 Douglas Frank 23 8 1 Bnejamin Zach 17 1 4 and store each value into a a separate variable. The names are strings, and the other numbers are floats and an int. I can get the data from one…
Erik Konoff
  • 11
  • 1
  • 3
1
vote
1 answer

New Compiler - Using Cout?

I've searched for a solution to my problem, and the posted ones don't seem to work. I am trying to run the following in Visual Studio 2012. I have previously used Eclipse for my programming, and am adjusting to the new IDE. class …
Rome_Leader
  • 2,518
  • 9
  • 42
  • 73
1
vote
2 answers

Endless loop in C while scanning file

I am trying to scan file by using while loop: while(feof(src_file) == 0){ } This method works perfectly fine if there is only one row in scanned file. Otherwise, I get an endless loop. Why is that and how to fix this issue?
user2205288
1
vote
1 answer

How to implement "press to enter"in c++

any idea on how to implement a "press any key to move on" in c++? Based on my understanding, for any input stream function, it all requires users to hit "enter" to read. But how do I make it like “whenever a key is being hit, it moves on to the next…
user1819047
  • 667
  • 9
  • 18
1
vote
1 answer

Combination of std::ios::openmode to avoid modifications of an existing file?

Is there an available combination of std::ios::openmode to avoid modifications of an existing file and allow only the creation of a new one ?
Vincent
  • 57,703
  • 61
  • 205
  • 388
1
vote
2 answers

Cannot include or it crashes

I am using eclipse with C++ and opengl. However in my program I cannot use #include or I get the following error The program file specified in the launch configuration does not exist C:\Users\workspace\mapCreator\Debug\mapCreator.exe not found The…
user1814893
1
vote
1 answer

ostream.write writes extra bytes into buffer

I'm using ostream to serialize an object, but the write() method seems to write extra bytes into the buffer. uint32_t id1=0x01; uint32_t id2=0xdeadbeef; std::stringstream sink; sink.write(reinterpret_cast
Wei Shi
  • 4,945
  • 8
  • 49
  • 73
1
vote
0 answers

File size computation benchmark: C two times slower than C++?

Consider the following code with 3 different versions of file size computation. #include #include #include #include inline long long int filesize1(const std::string& filename) { std::ifstream…
Vincent
  • 57,703
  • 61
  • 205
  • 388
1
vote
1 answer

Reading some integers then a line of text in C++

I'm reading input in a C++ program. First some integers, then a string. When I try reading the string with getline(cin,stringname);, it doesn't read the line that the user types: instead, I get an empty line, from when the user pressed Enter after…
Tal
1
vote
1 answer

how to know that no data available on boost::asio::ip::tcp::iostream?

I'm using boost::asio::ip::tcp::iostream to read binary data from TCP stream. I do this like that: stream.read(reinterpret_cast(&packetSize), 4); // first 4 bytes is length stream.read(buffer, packetSize); Should I just check stream.gcount()…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
1
vote
2 answers

derived basic_ostream: "using" keyword and ambiguous overload for operator <<

I have a derived basic_ostream class and an inline modifier (similar to setw). My stream class should also inherit all the operator << behavior from its parent. I get different compiler errors depending on whether I use the "using" keyword or…
John S
  • 3,035
  • 2
  • 18
  • 29
1 2 3
99
100