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
75
votes
7 answers

Are there binary memory streams in C++

I usually use stringstream to write into in-memory string. Is there a way to write to a char buffer in binary mode? Consider the following code: stringstream s; s << 1 << 2 << 3; const char* ch = s.str().c_str(); The memory at ch will look like…
FireAphis
  • 6,650
  • 8
  • 42
  • 63
75
votes
6 answers

How to write custom input stream in C++

I'm currently learning C++ (Coming from Java) and I'm trying to understand how to use IO streams properly in C++. Let's say I have an Image class which contains the pixels of an image and I overloaded the extraction operator to read the image from…
kayahr
  • 20,913
  • 29
  • 99
  • 147
74
votes
13 answers

How to solve "Unresolved inclusion: " in a C++ file in Eclipse CDT?

I download eclipse for c++ (cdt-master-8.0.2.zip). When I write: #include It marks: Unresolved inclusion: How can I fix it?
Adam Sh
  • 8,137
  • 22
  • 60
  • 75
69
votes
3 answers

How to get IOStream to perform better?

Most C++ users that learned C prefer to use the printf / scanf family of functions even when they're coding in C++. Although I admit that I find the interface way better (especially POSIX-like format and localization), it seems that an overwhelming…
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
68
votes
3 answers

Why use endl when I can use a newline character?

Is there a reason to use endl with cout when I can just use \n? My C++ book says to use endl, but I don't see why. Is \n not supported as widely as endl, or am I missing something?
Moshe
  • 57,511
  • 78
  • 272
  • 425
67
votes
7 answers

Why std::cout instead of simply cout?

I get these error messages for all cout and endl: main.cc:17:5: error: ‘cout’ was not declared in this scope main.cc:17:5: note: suggested alternative: /usr/include/c++/4.6/iostream:62:18: note: ‘std::cout’ After following the suggestion,…
erikbstack
  • 12,878
  • 21
  • 81
  • 115
64
votes
5 answers

Are int8_t and uint8_t intended to be char types?

Given this C++11 program, should I expect to see a number or a letter? Or not make expectations? #include #include int main() { int8_t i = 65; std::cout << i; } Does the standard specify whether this type can or will…
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
63
votes
4 answers

ifstream: check if opened successfully

A colleague just told me that this code: std::ifstream stream(filename.c_str()); if (!stream) { throw std::runtime_error(".."); } would be wrong. He said ifstream evaluates to 0 if opening is successful. My code works, but I wanted to find the…
Philipp
  • 11,549
  • 8
  • 66
  • 126
60
votes
6 answers

Obtain a std::ostream either from std::cout or std::ofstream(file)

how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the…
mavam
  • 12,242
  • 10
  • 53
  • 87
58
votes
7 answers

How to print __int128 in g++?

I am using the GCC built-in type __int128 for a few things in my C++ program, nothing really significant, at least not enough to justify to use BigInt library only for that and, yet, enough to prevent to remove it totally. My problem comes when I…
perror
  • 7,071
  • 16
  • 58
  • 85
58
votes
1 answer

Why can't std::ostream be moved?

Clearly, streams can't be copied. It should be possible to move streams. According to 27.9.1.11 [ofstream.cons] paragraph 4 it is possible to move construct an std::ofstream (the same is true for std::ifstream, std::fstream, and the…
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
57
votes
4 answers

Why do C++ streams use char instead of unsigned char?

I've always wondered why the C++ Standard library has instantiated basic_[io]stream and all its variants using the char type instead of the unsigned char type. char means (depending on whether it is signed or not) you can have overflow and underflow…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
57
votes
11 answers

How to easily make std::cout thread-safe?

I have a multi-threaded application, which heavily uses std::cout for logging without any locking. In such a case, how can I easily add lock mechanism to make std::cout thread-safe? I don't want to search for each occurrence of std::cout and add a…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
57
votes
7 answers

Standard no-op output stream

Is there a way to create an ostream instance which basically doesn't do anything ? For example : std::ostream dummyStream(...); dummyStream << "Nothing will be printed"; I could just create an ostringstream, but data will be buffered (and I really…
Maël Nison
  • 7,055
  • 7
  • 46
  • 77
56
votes
2 answers

What is the C++ iostream endl fiasco?

I was listening to a google talk by Andrei Alexandrescu on the D programming language when he threw out a one liner about the "endl" fiasco. I just thought endl was the preferred way to signify the end of a line and flush the buffer for a stream.…
Tod
  • 8,192
  • 5
  • 52
  • 93