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
31
votes
3 answers

When should I concern myself with std::iostream::sentry?

Online references have rather brief and vague descriptions on the purpose of std::iostream::sentry. When should I concern myself with this little critter? If it's only intended to be used internally, why make it public?
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
30
votes
2 answers

Do I have to use #include beside ?

I started learning C++ and I read a book which writes that I must use the header file because the string type is not built directly into the compiler. If I use the I can use the string type. Do I have to include the
KOB
  • 1,156
  • 1
  • 16
  • 33
29
votes
7 answers

checking data availability before calling std::getline

I would like to read some data from a stream I have using std::getline. Below a sample using the std::cin. std::string line; std::getline( std::cin, line ); This is a blocking function i.e. if there is no data or line to read it blocks…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
29
votes
5 answers

Why do C++ standard file streams not follow RAII conventions more closely?

Why do C++ Standard Library streams use open()/close() semantics decoupled from object lifetime? Closing on destruction might still technically make the classes RAII, but acquisition/release independence leaves holes in scopes where handles can…
Jeff
  • 3,475
  • 4
  • 26
  • 35
28
votes
5 answers

Reading and writing a std::vector into a file correctly

That is the point. How to write and read binary files with std::vector inside them? I was thinking something like: //============ WRITING A VECTOR INTO A FILE ================ const int DIM = 6; int array[DIM] = {1,2,3,4,5,6}; std::vector
FacundoGFlores
  • 7,858
  • 12
  • 64
  • 94
27
votes
11 answers

Alternative function in iostream.h for getch() of conio.h?

I'm trying to hold the screen on my output using the header file , but I don't know any equivalent function to the getch() & clrscr() functions of in or any other C++ library. Are there any such functions?
Aayush
  • 3,079
  • 10
  • 49
  • 71
26
votes
1 answer

Using char16_t and char32_t in I/O

C++11 introduces char16_t and char32_t to facilitate working with UTF-16- and UTF-32-encoded text strings. But the library still only supports the implementation-defined wchar_t for multi-byte I/O. Why has support for char16_t and…
oz1cz
  • 5,504
  • 6
  • 38
  • 58
25
votes
1 answer

Why does the rvalue overload of `operator<<` for `basic_ostream` return an lvalue reference?

§27.7.3.9 defines the following overload for operator<<: template basic_ostream& operator<<(basic_ostream&& os, const T& x); Effects: os << x Returns: os (§27.7.2.6 defines…
Xeo
  • 129,499
  • 52
  • 291
  • 397
25
votes
4 answers

Typo with "cout < myint". Why does it work?

I have this code and I searched for hours why it fails to print my income int const income = 0; std::cout << "I'm sorry, your income is: " < income; Until I found I missed to write << but wrote <. Why doesn't the compiler detect this and error out?…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
25
votes
4 answers

What's the difference between while(cin) and while(cin >> num)

What the difference between the following two loops and When each one will stopped ? #include #include #include using namespace std; int main() { int x,y; while(cin >> x){ // code } while(cin){ …
Ahmed_Mohsen
  • 285
  • 1
  • 3
  • 6
24
votes
3 answers

Why is the address of this volatile variable always at 1?

I wanted to inspect the address of my variable volatile int clock; cout << &clock; But it always says that x is at address 1. Am i doing something wrong??
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
24
votes
3 answers

Resetting the End of file state of a ifstream object in C++

I was wondering if there was a way to reset the eof state in C++?
Steffan Harris
  • 9,106
  • 30
  • 75
  • 101
24
votes
5 answers

Loading a file into a vector

I would like to load the contents of a text file into a vector (or into any char input iterator, if that is possible). Currently my code looks like this: std::vector vec; std::ifstream file("test.txt"); assert(file.is_open()); while…
Mankarse
  • 39,818
  • 11
  • 97
  • 141
24
votes
3 answers

How do the stream manipulators work?

It is well known that the user can define stream manipulators like this: ostream& tab(ostream & output) { return output<< '\t'; } And this can be used in main() like this: cout<<'a'<
Narek
  • 38,779
  • 79
  • 233
  • 389
24
votes
3 answers

How to create a boost ssl iostream?

I'm adding HTTPS support to code that does input and output using boost tcp::iostream (acting as an HTTP server). I've found examples (and have a working toy HTTPS server) that do SSL input/output using boost::asio::read/boost::asio::write, but none…
gavinandresen
  • 551
  • 1
  • 4
  • 11