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
55
votes
4 answers

What serious alternatives exist for the IOStream library? (besides cstdio)

I'm looking for a library which operates similar to iostreams, in that it performs conversions, and allows writing to memory buffers, files, and the console. However, I'd like something type safe, as iostream is. Are there any serious libraries…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
55
votes
4 answers

Why are C++ STL iostreams not "exception friendly"?

I'm used to the Delphi VCL Framework, where TStreams throw exceptions on errors (e.g file not found, disk full). I'm porting some code to use C++ STL instead, and have been caught out by iostreams NOT throwing exceptions by default, but setting…
Roddy
  • 66,617
  • 42
  • 165
  • 277
54
votes
3 answers

What does the "c" mean in cout, cin, cerr and clog?

What does the "c" mean in the cout, cin, cerr and clog names? I would say char but I haven't found anything to confirm it.
Rexxar
  • 1,886
  • 1
  • 14
  • 19
52
votes
6 answers

Are there any tricks to use std::cin to initialize a const variable?

Common std::cin usage int X; cin >> X; The main disadvantage of this is that X cannot be const. It can easily introduce bugs; and I am looking for some trick to be able to create a const value, and write to it just once. The naive solution //…
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
51
votes
4 answers

Using flush() before close()

As per the java docs, invoking close() on any java.io Streams automatically invokes flush(). But I have seen in lot of examples, even in production codes, developers have explicitly used flush() just before close(). In what conditions we need to…
Siva Arunachalam
  • 7,582
  • 15
  • 79
  • 132
50
votes
3 answers

Difference between iostream and iostream.h

What is the difference between iostream and iostream.h?
ckv
  • 10,539
  • 20
  • 100
  • 144
50
votes
2 answers

c++ multiple definitions of operator<<

I am attempting to override the << operator for a class. The purpose is basically to implement a toString() like behavior for my class, so that sending it to cout will produce useful output. Using a dummy example, I have the code below. When I…
ewok
  • 20,148
  • 51
  • 149
  • 254
48
votes
5 answers

Byte Stream and Character stream

Please explain what Byte streams and Character streams are. What exactly do these mean? Is a Microsoft Word document Byte oriented or Character oriented? Thanks
JavaUser
  • 25,542
  • 46
  • 113
  • 139
45
votes
1 answer

Why is "Init" in std::ios_base::Init uppercase?

All the names in the standard C++ library are lowercase except std::ios_base::Init. Why is this?
template boy
  • 10,230
  • 8
  • 61
  • 97
44
votes
4 answers

std::cout won't print

Is there any circumstance when std::cout << "hello" doesn't work? I have a C++ program where std::cout doesn't seem to print anything, not even constant strings (such as "hello"). Is there any way to check if cout is able/unable to open the stream?…
mahmood
  • 23,197
  • 49
  • 147
  • 242
42
votes
5 answers

DataContractSerializer - how can I output the xml to a string (as opposed to a file)

I had a quick question regarding the datacontractserializer. Maybe it's more of a stream question. I found a piece of code that writes the xml to a filestream. I basically don't want the file and just need the string output. public static string…
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
42
votes
4 answers

Prevent scientific notation in ostream when using << with double

I need to prevent my double to print in scientific notation in my file, when I do this outfile << X;
DogDog
  • 4,820
  • 12
  • 44
  • 66
40
votes
2 answers

how to convert System.IO.Stream into string and then back to System.IO.Stream

I have property of Stream type public System.IO.Stream UploadStream { get; set; } How can I convert it into a string and send on to other side where I can again convert it into System.IO.Stream?
SOF User
  • 7,590
  • 22
  • 75
  • 121
39
votes
7 answers

Reading a file character by character in C

I'm writing a BF interpreter in C and I've run into a problem reading files. I used to use scanf in order to read the first string, but then you couldn't have spaces or comments in your BF code. Right now here is what I have. char *readFile(char…
Devan Buggay
  • 2,717
  • 4
  • 26
  • 35
38
votes
6 answers

How can I detect if a type can be streamed to an std::ostream?

I'm trying to write a type trait to detect if a type has overloaded operator<<() suitable to use to an output stream. I'm missing something because I'm always getting true for a simple empty class with no operators at all. Here the…
gigabytes
  • 3,104
  • 19
  • 35