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
105
votes
6 answers

operator << must take exactly one argument

a.h #include "logic.h" ... class A { friend ostream& operator<<(ostream&, A&); ... }; logic.cpp #include "a.h" ... ostream& logic::operator<<(ostream& os, A& a) { ... } ... When i compile, it says: std::ostream& logic::operator<<(std::ostream&,…
As As
  • 2,049
  • 4
  • 17
  • 33
104
votes
10 answers

How to read until EOF from cin in C++

I am coding a program that reads data directly from user input and was wondering how could I (without loops) read all data until EOF from standard input. I was considering using cin.get( input, '\0' ) but '\0' is not really the EOF character, that…
Guille
  • 63
  • 2
  • 5
  • 10
104
votes
9 answers

How to read a file line by line or a whole text file at once?

I'm in a tutorial which introduces files (how to read from file and write to file) First of all, this is not a homework, this is just general help I'm seeking. I know how to read one word at a time, but I don't know how to read one line at a time,…
Mody
  • 1,267
  • 3
  • 10
  • 11
103
votes
2 answers

What exactly is streambuf? How do I use it?

I'm trying to learn a bit more about how I/O streams work in C++, and I'm really confused at when to use what. What exactly is a streambuf? When do I use a streambuf, as compared to a string, an istream, or a vector? (I already know the last three,…
user541686
  • 205,094
  • 128
  • 528
  • 886
97
votes
3 answers

What is the header?

What's the header used for? Why is it necessary? Any example?
wp2
  • 1,321
  • 3
  • 11
  • 10
87
votes
2 answers

'cout' was not declared in this scope

I have a C++ program: test.cpp #include int main() { char t = 'f'; char *t1; char **t2; cout<
user494461
85
votes
3 answers

Big difference (x9) in the execution time between almost identical code in C and C++

I was trying to solve this exercise from www.spoj.com : FCTRL - Factorial You don't really have to read it, just do it if you are curious :) First I implemented it in C++ (here is my solution): #include using namespace std; int main()…
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
84
votes
9 answers

How can I print 0x0a instead of 0xa using cout?

How can I print 0x0a, instead of 0xa using cout? #include using std::cout; using std::endl; using std::hex; int main() { cout << hex << showbase << 10 << endl; }
Ayrosa
  • 3,385
  • 1
  • 18
  • 29
83
votes
17 answers

Output Unicode strings in Windows console

Hi I was trying to output unicode string to a console with iostreams and failed. I found this: Using unicode font in c++ console app and this snippet works. SetConsoleOutputCP(CP_UTF8); wchar_t s[] = L"èéøÞǽлљΣæča"; int bufferSize =…
Andrew
  • 2,309
  • 4
  • 27
  • 42
83
votes
6 answers

How do I deal with the max macro in windows.h colliding with max in std?

So I was trying to get valid integer input from cin, and used an answer to this question. It recommended: #include // includes WinDef.h which defines min() max() #include using std::cin; using std::cout; void Foo() { int…
Almo
  • 15,538
  • 13
  • 67
  • 95
82
votes
3 answers

Converting ostream into standard string

I am very new to the C++ STL, so this may be trivial. I have a ostream variable with some text in it. ostream* pout; (*pout) << "Some Text"; Is there a way to extract the stream and store it in a string of type char*?
Stephen Diehl
  • 8,271
  • 5
  • 38
  • 56
82
votes
10 answers

How to print Unicode character in C++

I am trying to print a Russian "ф" (U+0444 CYRILLIC SMALL LETTER EF) character, which is given a code of decimal 1092. Using C++, how can I print out this character? I would have thought something along the lines of the following would work,…
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
79
votes
2 answers

What does the g stand for in gcount, tellg and seekg?

What does the g stand for in std::iostream's gcount, tellg and seekg members? And the p in pcount, tellp and seekp? Why aren't they called just count, tell and seek?
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
78
votes
6 answers

How to output a character as an integer through cout?

#include using namespace std; int main() { char c1 = 0xab; signed char c2 = 0xcd; unsigned char c3 = 0xef; cout << hex; cout << c1 << endl; cout << c2 << endl; cout << c3 << endl; } I expected…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
76
votes
9 answers

Java IO implementation of unix/linux "tail -f"

I'm wondering what techniques and/or library to use to implement the functionality of the linux command "tail -f ". I'm essentially looking for a drop in add-on/replacement for java.io.FileReader. Client code could look something like…
Gary
  • 6,357
  • 5
  • 30
  • 36