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
20
votes
13 answers

How to output to the console in C++/Windows

When using iostream in C++ on Linux, it displays the program output in the terminal, but in Windows, it just saves the output to a stdout.txt file. How can I, in Windows, make the output appear in the console?
Xunil
  • 295
  • 2
  • 4
  • 11
20
votes
2 answers

How to simulate printf's %p format when using std::cout?

unsigned char *teta = ....; ... printf("data at %p\n", teta); // prints 0xXXXXXXXX How can I print variable address using iostreams? Is there a std::??? feature like std::hex to do this kind of conversion (address -> string), so std::cout <<…
kagali-san
  • 2,964
  • 7
  • 48
  • 87
20
votes
1 answer

gcc: Strip unused functions

I noticed that sometimes even if I don't use iostream and related I/O libraries, my binaries produced by Mingw were still unreasonably large. For example, I wrote a code to use vector and cstdio only and compiled it with -O2 -flto, my program can go…
John London
  • 1,250
  • 2
  • 14
  • 32
20
votes
1 answer

Fast controlled copy from istream to ostream

I have to copy several bytes from a istream to a ostream, there are 2 ways that I know to perform this copy. myostream << myistream.rdbuf(); and copy( istreambuf_iterator(myistream), istreambuf_iterator(), …
Arkaitz Jimenez
  • 22,500
  • 11
  • 75
  • 105
20
votes
9 answers

Kill a blocked Boost::Thread

I am writing an application which blocks on input from two istreams. Reading from either istream is a synchronous (blocking) call, so, I decided to create two Boost::threads to do the reading. Either one of these threads can get to the "end" (based…
mmocny
  • 8,775
  • 7
  • 40
  • 50
20
votes
3 answers

iostream linker error

I have some old C code that I would like to combine with some C++ code. The C code used to have has the following includes: #include #include #include #include "mysql.h" Now I'm trying to make it use C++ with…
Steve
  • 1,955
  • 9
  • 28
  • 30
20
votes
5 answers

#include iostream in C?

In C++ we always put the following at the top of the program #include What about for C?
neuromancer
  • 53,769
  • 78
  • 166
  • 223
20
votes
5 answers

Why the constructor of std::ostream is protected?

Why I can't just create an "empty" stream for my output like this std::ostream out; ? This rows is apparently illegal with both clang 3.4 and gcc 4.8.1 under linux with libstdc++, I really don't get why, I mean why I can't just create a stream out…
user2485710
  • 9,451
  • 13
  • 58
  • 102
20
votes
2 answers

Reading and writing to the same file using the same fstream

I have a file that already contains some data (say, 8 kB). I want to read something from the beginning of the file, and then overwrite data starting where I finished reading. So I try to use the following code: std::fstream stream("filename",…
svick
  • 236,525
  • 50
  • 385
  • 514
20
votes
2 answers

c++ connect output stream to input stream

What I would like to do is create a sort of "pipe" (like a pipe between processes), but between c++ iostreams within the same program. I have a function that requires an input stream as an argument, but my data is coming from an output stream. So is…
Nick
  • 499
  • 5
  • 13
19
votes
5 answers

Detecting reason for failure to open an ofstream when fail() is true

Seems like this should be simple, but I don't find it in a net search. I have an ofstream which is open(), and fail() is now true. I'd like to know the reason for the failure to open, like with errno I would do sys_errlist[errno].
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
19
votes
1 answer

Why does my (C++) compiler want to instantiate my parameter pack class when using std::endl?

Consider the following short program. #include template< typename ... Ts > class foobar { static_assert( sizeof...(Ts) > 0 ); }; template< typename ... Ts > std::ostream& operator<<( std::ostream& o, const foobar< Ts... >& ) { …
Ted Middleton
  • 6,859
  • 10
  • 51
  • 71
19
votes
3 answers

Injecting string to 'cin'

I have a function that reads user input from std::cin, and I want to write a unittest that inserts some strings into std::cin, such that later extraction from std::cin will read that string instead of pausing for keyboard input. Ideally, I would…
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
19
votes
3 answers

How do I print out the contents of a file? C++ File Stream

I am using fstream and C++ and all I want my program to do is to print out to the terminal the contents of my .txt file. It may be simple, but I have looked at many things on the web and I can't find anything that will help me. How can I do this?…
Lucas Smith
  • 698
  • 2
  • 9
  • 17
19
votes
6 answers

C++ read from istream until newline (but not whitespace)

I have a std::istream which refers to matrix data, something like: 0.0 1.0 2.0 3.0 4.0 5.0 Now, in order to assess the number of columns I would like to have some code like: std::vector vec; double x; while( (...something...) && (istream >>…
StephQ
  • 2,032
  • 3
  • 19
  • 27