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

inheriting iostream and streambuf gives a wierd starting address msg

I have a class logger that implementes streambuf and iostream and overrides xsputn and overflow.. This has been working for a while now... class Logger : public std::iostream, public std::streambuf { virtual std::streamsize xsputn(const char* s,…
Alon
  • 1,776
  • 13
  • 31
1
vote
4 answers

one question about iostream cout in C++

In such code, what it is called, \\n like this? cout<<"Hello\\n \'world\'!"; What's the basic rule about such characters?
skydoor
  • 25,218
  • 52
  • 147
  • 201
1
vote
2 answers

Read text file display to console then append text file

I have a text file of names. I want to read the text file into a stream, display it to the console. When it is done, it will prompt the user to enter their name. It should then add it to the file. I can get it to do both of these things separately…
Brandon
  • 915
  • 4
  • 23
  • 44
1
vote
1 answer

Modify input stream data on the fly

I would like to implement a std::stream modifier/parser, that is doing data manipulation on the fly. Is it possible to create it in form of stream manipulator? For example, i want to strip all the line comments (from any // to the end of line) out…
Frizi
  • 2,900
  • 1
  • 19
  • 25
1
vote
1 answer

C++ simple code issue about cout and cin

I have the following code. Everything works fine but quite different. I expect the first three cout << come one after the another, so when the first message is shown in the console, the user enters the value, and then the next cout << shows another…
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
1
vote
1 answer

Passing a StreamReader object by Reference

I'm creating a small file parsing console app using C# in VS 2013. I'm using StreamReader to read in the file and creating objects from data in the file by parsing the lines. I call a method, CreateHeader(), to create a Header object and pass the…
Shaun
  • 121
  • 4
  • 14
1
vote
1 answer

Possible to create a dual-seekable Boost Iostream using a file_descriptor?

I'm trying to update a random-access binary file using the std::iostream interface with separate get/put positions managed via seekg/seekp. Everything works fine with stringstream, but when I create a file descriptor-based stream using…
Trevor Robinson
  • 15,694
  • 5
  • 73
  • 72
1
vote
2 answers

inserting text before each line using std::ostream

I would like to know if it is possible to inherit from std::ostream, and to override flush() in such a way that some information (say, the line number) is added to the beginning of each line. I would then like to attach it to a std::ofstream (or…
John
  • 1,709
  • 1
  • 24
  • 27
1
vote
1 answer

Prevent a big number from being formated like this "24.000.000"

I am inserting some ints in my ostream but big numbers or even years, like 3000 or 25000123 are being formatted like this 3.000 or 25.000.123. I am not sure why this is happening. It might be because I usedd imbue("") on the stream so decimal…
dabadaba
  • 9,064
  • 21
  • 85
  • 155
1
vote
1 answer

Orwell Dev-C++ Fatal Error: "iostream: no such file or directory"

I am attempting to compile a header file in Orwell Dev-C++ that was written on another machine. I've just downloaded Dev-C++ so I'm not very familiar with it. Below is a snippet of my code that is causing a problem: #ifndef JOB_H #define…
B4dmonkey
  • 112
  • 1
  • 2
  • 12
1
vote
3 answers

Problem with iostream, my output endl are littles squares

I have a problem with with my output when I write to I file I get squares when I put endl to change lines. std::ofstream outfile (a_szFilename, std::ofstream::binary); outfile<<"["<
DogDog
  • 4,820
  • 12
  • 44
  • 66
1
vote
1 answer

Appending to existing file via boost

I need to aggregate many log files into a single log. I tried to do this with boost::filesystem::copy_file but it doesn't support appending. Any ideas? (I'm preferring doing this via boost libraries) Tnx
o.z
  • 1,086
  • 14
  • 22
1
vote
1 answer

std::ostream to file or standard output

I would like to write my output to a file if a file name is avaliable or on the screen (stdout) otherwise. So I've read posts on this forum and found a code, which below I wrapped into a method: std::shared_ptr out_stream(const…
tnorgd
  • 1,580
  • 2
  • 14
  • 24
1
vote
1 answer

fscanf vs ifstream speed

I conjectured that ifstream would be faster than fscanf because fscanf has to parse the format string every time it runs, whereas, with ifstream, we know at compile time what kind of "thing" we want to read. But when I ran this quick and dirty…
math4tots
  • 8,540
  • 14
  • 58
  • 95
1
vote
2 answers

ImageIo.read method doesn't work in android

ImageIo.read() doesn't work.the error tells that ImageIo class doesn't contains read method.like ByteArrayInputStream bais=new ByteArrayInputStream(logo); BufferedImage bimage=ImageIO.read(bais); any one help me?
user3543997
  • 121
  • 1
  • 1
  • 7