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

std:cout within function not printing (FIXED:for loop issue)

NEVERMIND, FIXED IT. for loop issue I'm trying to do some number crunching with formatted output, and I've run into a problem with half my output not printing. I've written a small test code to illustrate the problem: #include int…
user3083602
1
vote
1 answer

How do I handle a single text io stream with multiple inputs and outputs?

So I'm working through a bit of a problem, and some advice would be nice. First a little background, please excuse the length. I am working on a management system that queries network devices via the TL1 protocol. For those unfamiliar with the…
Bill
  • 2,623
  • 1
  • 27
  • 41
1
vote
3 answers

How do I close a socket or an I/O Stream in the windowClosing event of a Java window?

I'm having trouble closing my socket when exiting my Java application. I thought that an easy way to make sure the socket gets closed is to hook it on windowClosing in a Swing JFrame like this: frame.addWindowListener(new WindowAdapter() { …
neapsix
  • 11
  • 2
1
vote
1 answer

Should Input stream be closed too after Buffered Reader is closed?

In a very usual operation of parsing JSON, we do something like this StringBuilder sb = new StringBuilder(); BufferedReader br = null; try { URL requestUrl = new URL(url); URLConnection con =…
Ankit
  • 4,426
  • 7
  • 45
  • 62
1
vote
1 answer

Solving valgrind memory error not pointing to my code

Valgrind gives me this: ==26348== Invalid read of size 8 ==26348== at 0x4EC3DA8: std::ostream::flush() (in /usr/lib64/libstdc++.so.6.0.19) ==26348== by 0x4E9E477: std::ios_base::Init::~Init() (in /usr/lib64/libstdc++.so.6.0.19) ==26348== …
shane
  • 1,742
  • 2
  • 19
  • 36
1
vote
2 answers

Creating large stream in memory in c++

I am using an external library which is creating a large amount (>4GB)of binary data. I want to buffer this data and feed it to a second library while it is still being created. My program is a 64 bit process, running on Linux. I cannot make any…
Heinrich Heine
  • 303
  • 3
  • 12
1
vote
1 answer

Simple threaded server implemented by gio library

I am trying to learn gio library, especially the giostream and gthreadedsocketservice. I want to write a simple server that: Each incoming connection will be handled by an individual new thread At the client side, user types a string and it will be…
robinchm
  • 109
  • 1
  • 1
  • 7
1
vote
1 answer

How do i create a class that manipulates texts when using cout?

I want to create a manipulator that delays between every character like if i write delay wait = 40; cout << wait << "Hello World!"; It should output 'H' then Sleep(40), 'e' then Sleep(40), 'l' then Sleep(40) and so on, i tried to write a class for…
Brogramer
  • 75
  • 8
1
vote
2 answers

std::cin while loop gives a strange result

As of late, I've been doing a complete review of C++ and came across a code snippet containing the following: #include using namespace std; int main() { int a, b; while (cin >> a) { b+=a; } cout << b << endl; return…
Heatherfield
  • 185
  • 4
1
vote
1 answer

How to clear all sticky manipulators on (string)stream?

I am clearing a std::stringstream the usual way: std::ostringstream ss; for(...; ...; ...) { ... // Use ss. if(some_condition_to_reset_stringstream) { ss.str(std::string()); ss.clear(); } ... // Use ss some…
zennehoy
  • 6,405
  • 28
  • 55
1
vote
2 answers

Can't get fstream to seekg back to 0 after EOF flag set

So I've got this fstream that I'm reading from my file with, and I get this insane bug when trying to read from my file after the EOF flag is set (or at least that's what I think is happening). This is the scope of my problem: if (!reader.good()) …
Scott Steinbach
  • 307
  • 1
  • 5
1
vote
4 answers

Input/output stream abstraction layers for plain C

Are there any widely used I/O stream abstraction layers for plain C? By an I/O stream abstraction layer I mean any layer that at least allows the creation of custom read/write functions. For C++, there's the standard iostream and boost::iostreams.…
ahnurmi
  • 116
  • 1
  • 5
1
vote
1 answer

Unable to print CSV file

The following code compiles fine so there's no syntax errors. I am having trouble printing out the CSV file I read into this bit of code. I am not sure what is going on as this is a standard procedure, but would appreciate some input as to how to…
1
vote
0 answers

Python – BinaryIO Stream read while writing

I need a stream in Python that is writing stuff. But while it is writing it, a threaded method should read from it, until it gets closed (EOF). So far I can only create a stream with stream = io.BinaryIO('binarystart') and then stream.seek(0, 1) to…
max
  • 318
  • 1
  • 11
1
vote
2 answers

STL Map - Displaying what is pointed to by find() function

For testing purposes, I'm running the following code through a for loop. Only the first three keys actually exist, and "Record found" is displayed as expected, along with the key, retrieved from findVertex->first. My question is, how would I be…