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
37
votes
2 answers

Include of iostream leads to different binary

Compiling the following code int main() { return 0; } gives the assembly main: xorl %eax, %eax ret https://gcc.godbolt.org/z/oQvRDd If now iostream is included #include int main() { return 0; } this…
schorsch312
  • 5,553
  • 5
  • 28
  • 57
37
votes
2 answers

Find all substring's occurrences and locations

I'm writing a program to parse some data saved as text files. What I am trying to do is find the location of every needle in a haystack. I already can read the file in and determine the number of occurrences, but I am looking to find the index also.
Thomas Havlik
  • 1,378
  • 4
  • 12
  • 20
37
votes
7 answers

Overload handling of std::endl?

I want to define a class MyStream so that: MyStream myStream; myStream << 1 << 2 << 3 << std::endl << 5 << 6 << std::endl << 7 << 8 << std::endl; gives output [blah]123 [blah]56 [blah]78 Basically, I want a "[blah]" inserted at the front, then…
anon
  • 41,035
  • 53
  • 197
  • 293
37
votes
9 answers

vs. vs. "iostream.h"

When including a header file in C++, what's the difference between... including the .h part versus not including .h part when wrapping it in <> signs? #include vs. #include wrapping the header name in double quotes versus…
BeachRunnerFred
  • 18,070
  • 35
  • 139
  • 238
37
votes
2 answers

When should I use string instead of stringstream?

When should I use stringstream instead of string::append()? Supposing I'm going to catenate just strings. stringstream ss; ss << str1 << "str2" << ... Write(ss.str()); Or: string…
Lucas Lima
  • 1,465
  • 2
  • 16
  • 28
36
votes
5 answers

Android ndk-build iostream: No such file or directory

I'm having a problem with compiling a .cpp file using the ndk-build tool (Windows 7 with Cygwin). The error appears when I try to compile the .cpp file with #include: jni/native.cpp:5:20: error: iostream: No such file or directory Here is my .cpp…
Andrey Zavarin
  • 1,483
  • 2
  • 13
  • 12
36
votes
5 answers

In Java, how can I redirect System.out to null then back to stdout again?

I've tried to temporarily redirect System.out to /dev/null using the following code but it doesn't work. System.out.println("this should go to stdout"); PrintStream original = System.out; System.setOut(new PrintStream(new…
dgrant
  • 1,417
  • 3
  • 16
  • 23
35
votes
4 answers

How to read a complete line from the user using cin?

Here is my current C++ code. I would like to know how to write a line of code. Would I still use cin.getline(y) or something different? I've checked, but can't find anything. When I run it, it works perfectly except it only types one word instead of…
FuzionSki
  • 451
  • 1
  • 6
  • 8
35
votes
8 answers

Printing double without losing precision

How do you print a double to a stream so that when it is read in you don't lose precision? I tried: std::stringstream ss; double v = 0.1 * 0.1; ss << std::setprecision(std::numeric_limits::digits10) << v << " "; double u; ss >> u; std::cout <<…
Martin York
  • 257,169
  • 86
  • 333
  • 562
35
votes
13 answers

Which C I/O library should be used in C++ code?

In new C++ code, I tend to use the C++ iostream library instead of the C stdio library. I've noticed some programmers seem to stick to stdio, insisting that it's more portable. Is this really the case? What is better to use?
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
34
votes
5 answers

cstdio streams vs iostream streams?

I just learned of the existence of the ios_base::sync_with_stdio function, which basically allows you to turn off (or on if you already turned it off) the synchronization between iostream streams that are used in C++ and the cstdio streams that are…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
34
votes
1 answer

Why is std::streamsize defined as signed rather than unsigned?

According to http://en.cppreference.com/w/cpp/io/streamsize The type std::streamsize is a signed integral type used to represent the number of characters transferred in an I/O operation or the size of an I/O buffer. As far as I can imagine, a…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
34
votes
2 answers

Unexpected results with std::ofstream binary write

I'm new to C++ std::stream and I'm making some tests. I have this simple code: int i = 10; char c = 'c'; float f = 30.40f; std::ofstream out("test.txt", std::ios::binary | std::ios::out); if(out.is_open()) { out<
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
32
votes
3 answers

Colored output in C++

Is there a way to print colored output using iostream and Xcode? I'd like to be able to, for example, print Hello World! with Hello red, World blue and ! yellow. How can I do that?
Shoe
  • 74,840
  • 36
  • 166
  • 272
31
votes
10 answers

C++ Streams vs. C-style IO?

I was coding some C++ for a small hobby project when I noticed that I'm using C-style operations to access IO (printf, fopen, etc.). Is it considered "bad practice" to involve C functions in C++ projects? What are the advantages of using streams…
gablin
  • 4,678
  • 6
  • 33
  • 47