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

Unformatted output in operator <<

I have a class that contains decoded video frames. I would like my decoder to use an output_iterator to write those frames to different targets. In order to support writing directly to a file, I want to overload operator << for my decoded frame…
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
1
vote
3 answers

Is it possible to define enumalpha?

I would like to be able to write: cout << enumalpha << Monday; and get printed on console: Monday P.S. Monday is an enum type.
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
1
vote
1 answer

Is it possible to tie a C++ output stream to another output stream?

Is it possible to tie a C++ output stream to another output stream? I'm asking because I've written an ISAPI extension in C++ and I've written ostreams around the WriteClient and ServerSupportFunction/HSE_REQ_SEND_RESPONSE_HEADER_EX functions - one…
pyrachi
  • 800
  • 6
  • 15
1
vote
1 answer

How to output to console window without iostream in c++?

I'm a beginner C++ programmer.I would like to know that Is it possible to output to console windows without using iostream header file? the answer of the question is actually Yes ! but How?
Amirreza
  • 21
  • 3
1
vote
0 answers

Conditions for if statement using cin not satisfied, body of statement executes anyway

if (cin >> var){ //code 1 } else{ //code 2 } This is an example of my code. var is of type int, so I don't want code 1 to execute when the user inputs non-int values. If I enter a letter or a symbol, code 1 will not execute and code 2 will…
ozzymado
  • 960
  • 3
  • 15
  • 26
1
vote
1 answer

fstream - cannot access private members declared in class basic_fstream<_Elem, _Traits>

I use fstream to open a file for writing purpose. fstream streamFile; streamFile.open ( "C:\\path\\to\\textFile.txt", fstream::in | fstream::out| fstream::app); I get the following error: cannot access private members declared in class…
yael aviv
  • 201
  • 1
  • 5
  • 9
1
vote
0 answers

Why do c++ I/O streams have a conversion operator to void*?

I accidentally called operator delete[] on a ifstream object, instead of on a file name. I expected the compiler to issue an error because delete[] only operates on pointers, not objects. However, the compiler didn't complain (not even a…
anatolyg
  • 26,506
  • 9
  • 60
  • 134
1
vote
1 answer

Writing from an input file to an output file

I'm having troubles opening the my text file in this bit of code. Am I doing it the correct way? Just started C++ this week. I'm having troubles writing to the output file now. The only output I'm getting is this. libc++abi.dylib: terminating with…
MatthewTingle
  • 355
  • 2
  • 3
  • 12
1
vote
0 answers

Binary ofstream writes 1 byte instead of 4

The following small program is supposed to create a binary file, write 6 uint (typedef'ed as unsigned int) to it, and then read and print the data back from the same file. #include #include #include typedef unsigned…
1
vote
2 answers

I would like to create a function that reads each line of the input and produces its sum and save that as sum.txt using C++

Let's assume that I have the following input: 1 2 3 5 6 10 11 13 stored in wow.txt. I would like to create a function that reads each line of the input and produces its sum and save that as sum.txt using C++. In the input file, we have the…
user98235
  • 830
  • 1
  • 13
  • 31
1
vote
3 answers

How to have a file stream as a class member

I have the following parser class that works in Visual C++ class Parser { private: const char* filename; std::ifstream filestream; std::vector tokens; unsigned int linect; public: Parser(const char* filename); …
tomocafe
  • 1,425
  • 4
  • 20
  • 35
1
vote
1 answer

iostream keil c++ problems

I am getting some errors when trying to #include iostream. I know the problem is iostream because my project compiled before, but after including iostream I have errors. I am trying to use iostream because printf does not appear to work and I want…
user3729617
  • 113
  • 1
  • 14
1
vote
2 answers

std::getline and control characters

If I do: std::string buffer; std::getline(std::cin, buffer); Despite many niceties missing, that, say, libreadline provides, I am still able to use backspace and ENTER to enter my string. I am not sure whose responsibility it is to process these…
user1095108
  • 14,119
  • 9
  • 58
  • 116
1
vote
2 answers

Write Wave header and Add Data, without closing ofstream

As topic says, here is the code: template void writeWAVHeader ( char const* outFile, SampleType* buf, int sizeMult, size_t bufSize, int sampleRate, short channels, int Flush ) { if (sizeMult != 0) bufSize =…
Zerowalker
  • 761
  • 2
  • 8
  • 27
1
vote
2 answers

How to remove the leading white spaces but keep middle spaces in C++ `ifstream`?

For example, I have a file with the following contents: Hello John Smith Hello Jack Brown OK I love you Note that each sentence has some leading white spaces. I want to use std::fstream to read them line by…
xmllmx
  • 39,765
  • 26
  • 162
  • 323