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
1
vote
1 answer

Installed Cygwin64 and cannot find unresolved inclusion in when in eclipse

I linked to as many of the libraries as I can find and still I get an unresolved inclusion for in eclipse. I even searched the c:\cygwin\ directory for iostream and I cannot find it. So, how do I find this basic library in the cygwin64…
user2097211
  • 333
  • 2
  • 5
  • 16
1
vote
1 answer

How do I declare a custom char_traits<> for my own type?

I want to make a custom char_traits class for my own type. I have declared all the functions, but I am having some confusion regarding vague semantics given in the standard. What are fpos_type, off_type and state_type expected to do? Where are they…
Anurag Kalia
  • 4,668
  • 4
  • 21
  • 28
1
vote
2 answers

Program will not print line before exit() function?

I have a little project for school I am writing in C++, and we have to account for some error and just exit the program in the event that it happens. Basically, in the else statement when the expression is evaluated as false, it's like it won't…
Eric Diviney
  • 327
  • 2
  • 5
  • 16
1
vote
0 answers

Custom strbuf doesn't put anything in the file

I am trying to make a custom strbuf that takes in 32-byte characters and puts them in another ostream object in bytes. Following is the code for it: I personally suspect the way I have linked it in main.cpp though I can't pinpoint the error in it.…
Anurag Kalia
  • 4,668
  • 4
  • 21
  • 28
1
vote
1 answer

Why can I overload istream's operator>> for strings?

Suppose I wanted operator>> to extract entire lines from an istream instead of whitespace-separated words. I was surprised to see that this, although horrible, actually worked: #include #include namespace std { istream…
Thomas
  • 174,939
  • 50
  • 355
  • 478
1
vote
3 answers

Print exponential notation with one leading zero with C++

I am generating a text file to be used as a FORTRAN input file. The FORTRAN program specifies that the values it reads must be in a format such that 1.0 must be printed as 0.1000000E+01 As of right now the closest I have gotten in using iostream…
rhodysurf
  • 357
  • 1
  • 4
  • 12
1
vote
2 answers

Writing a C++ iostream that uses the RC4 stream cipher. How can I optimize my implementation?

I am implementing a custom iostream (i.e., with read, write, seek and close) which uses the RC4 stream cipher for encryption and decryption. One of the contracts of this stream is that it is bidirectional and calling code needs to be able to…
Ben J
  • 1,367
  • 2
  • 15
  • 33
1
vote
1 answer

Is the plus/minus sign internationalized?

I've been looking for a way to add my own custom sign character to positive and negative numbers, but there doesn't seem to be a standard way to do so. Is the plus minus sign internationalized in IOStreams? The closest thing I could find to…
David G
  • 94,763
  • 41
  • 167
  • 253
1
vote
0 answers

Convert ostream to iostream

I have two functions, let's call them producer(std::ostream& output) and consumer(std::istream& input), and I need to link the output of producer into the consumer. Right now I'm doing it this way: std::stringstream…
cube
  • 3,867
  • 7
  • 32
  • 52
1
vote
4 answers

What are the problems connected to data alignment on different architectures?

Following this comment to one of my previous questions, I was convinced that defining a struct, with fields having an appropriate type with a know and well defined size, and feeding an instance of this struct to read, was enough to read data from a…
user2485710
  • 9,451
  • 13
  • 58
  • 102
1
vote
1 answer

Why is this file reading code generating exceptions?

I have a function (addShape) to read ints from a file according to the id it gets. It gets the id and the stream is as parameters. for some reason I get thrown with std::ios_base::failure after reading the last line. while (is >> id) …
Quaker
  • 1,483
  • 3
  • 20
  • 36
1
vote
4 answers

A cleaner way to convert a string to int after checking for hex prefix?

This little exercise is meant to get a string from the user that could be decimal, hexadecimal, or octal. 1st I need to identify which kind of number the string is. 2nd I need to convert that number to int and display the number in its proper…
user2904033
  • 133
  • 1
  • 3
  • 13
1
vote
2 answers

Performant way of formatting numbers according to the user's locale with iostream?

In one part of our application there is the requirement to format numbers according to the user's locale. The interface essentially looks like this: std::string format_number(double number); The old implementation looked like this: std::string…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
1
vote
1 answer

Why does std::num_put take the ios_base parameter by non-const reference?

I'm experimenting with the iostreams / locale numeric facet and I've hit something quite curious: The "canonical example" of using the std::num_put facet to directly format a number goes like this: std::string f(double value) { using namespace…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
1
vote
2 answers

Can getline() be used to get a char array from a fstream

I want to add a new (fstream) function in a program that already uses char arrays to process strings. The problem is that the below code yields strings, and the only way i can think of getting this to work would be to have an intermediary function…
v_a_bhatia
  • 125
  • 1
  • 3
  • 12