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

How to keep reading using cin until a blank line is reached

I am trying to use standard input(cin) to read in inputs until a blank line is hit. I tried many times but still fail to achieve it. Can anyone help me out? The following is the input format. Note: 1. // are comments 2. Comments can be randomly…
Zhengxi Tan
  • 23
  • 2
  • 6
1
vote
1 answer

iostream GCC errors, converting to boost::filesystem::iostream for Windows

I'm attempting to convert an app written in C++14 for linux/MacOS. It uses boost::filesystem, but not for certain iostream operations. For example: boost::filesystem::path file = name; std::ifstream fin(file.c_str()); This code was failing to…
Brad
  • 1,684
  • 4
  • 20
  • 36
1
vote
1 answer

How to portably obtain the offset in bytes from the beginning of an istream?

We are reading into a file by accessing it through a std::ifstream, let's call it ifs. In our current procedure, we make a few formatted input operations (i.e. using operator>>()), which leads us to a given position in the stream, let's call it…
Ad N
  • 7,930
  • 6
  • 36
  • 80
1
vote
2 answers

How to use input stream overloading to insert item to map member in class?

I have C++ class Question to hold data from a file questions.txt of multiple choice questions and answers: update: I have updated the &operator>> operator overload I have one: it only insert first multiple choice question of 2 multiple choice…
NinjaDeveloper
  • 1,620
  • 3
  • 19
  • 51
1
vote
1 answer

I/O Stream String Manipulation (Correct Cin/cout operators <>)

I came across this question on here and had a further question about the answer give (I can't comment since I'm new to stackoverflow). The guy who answered it seems to be right about replacing << >> for cin and cout. But the problem I'm having is…
Deegeeek
  • 25
  • 1
  • 5
1
vote
1 answer

Send multiple files per connection with boost iostream

I'm trying to make a streaming application using boost with iostream, but the server is not separating the image frame in the receive loop, is getting everything in one file(does not close the file, and continues to receive the other frames in the…
Melissia_M
  • 323
  • 2
  • 13
1
vote
1 answer

How to read only chars from ifstream and ignore numbers in c++?

So i have a file that has numbers and a bunch of characters and i wanna store them in my own data type which i call Grid which is basically a two dimensional vector with some useful features that allow me to go ahead and store data without worrying…
Leo wahyd
  • 207
  • 1
  • 14
1
vote
3 answers

C++: Error on ostream << operator

I don't understand why this code gives me error void printSalesFile(vector< vector > & list) { ofstream outfile; outfile.open("sales.lst", ios::out); if (outfile.is_open()) { outfile << setw(6) << right << "ID" …
Jake quin
  • 738
  • 1
  • 9
  • 25
1
vote
3 answers

testing an istream object

When I use an std::istream object (in the example below from cplusplus.com, an std::ifstream) in a test : "if (myistreamobject)", the object, which is automatically allocated in the stack is never null, right ?... in the example below, we are using…
Aminos
  • 754
  • 1
  • 20
  • 40
1
vote
0 answers

Does std::ostream store the output internally and how to get it?

Does the std::ostream store the bytes that has been sent to the output internally? If it does, is there any way to read them back? Let's say, I need to check what byte has been sent to the output stream 9 bytes ago in order to send it to the output…
BarbaraKwarc
  • 2,667
  • 2
  • 14
  • 17
1
vote
2 answers

Check in a program if stderr is redirected to stdout

From within a C++ program, is it possible to check whether stderr is redirected into stdout or vice versa? Basically I want to know if those two file descriptors point to the same place. Platform-specific solutions using native APIs are OK.
usta
  • 6,699
  • 3
  • 22
  • 39
1
vote
1 answer

When overriding the << (stream) operator, debug goes into std's << impl instead?

This question has been partially answered: the solution is that in one of my class's fields, specifically _escape, has UB. However, it is still unknown why Visual Studio's stack trace does not display the operator<< function nor does the debugger…
OneRaynyDay
  • 3,658
  • 2
  • 23
  • 56
1
vote
4 answers

Same C++ code results in infinite loop on Windows and expected behavior on OSX

This is one of the weirder things I've seen. I'm teaching an intro C++ course at a university, and one of my students contacted me saying that his code was running forever without stopping. I briefly glanced over his code during class, and didn't…
Glen Balliet
  • 1,097
  • 2
  • 12
  • 21
1
vote
1 answer

How to read a fixed number of bytes from a c++ std::istream

How to read a fixed number of bytes from a std::istream without doing any extraction? For example, I have a variable sz of type size_t and I would like to read sizeof(size_t) bytes from the istream. void foo(std::istream& is) { …
Jes
  • 2,614
  • 4
  • 25
  • 45
1
vote
2 answers

Swapping a stringstream for cout

With glibc's stdio, I can swap a memstream for stdout, thereby capturing the output of a piece of code compiled to output to stdout: #include void swapfiles(FILE* f0, FILE* f1){ FILE tmp; tmp = *f0; *f0 = *f1; *f1 = tmp; } void hw_c(){…
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142