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

Is there a way to check if an istream was opened in binary mode?

I'm using an istream which could be stringstream, ifstream or a user-defined stream type and I need to know if, in the case of an ifstream, it was not opened in binary mode (so I can throw an exception). I have tried the following method: if…
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
23
votes
4 answers

Why are C++ iostreams always left-hand assigned?

I am learning C++ and have never dealt with streams. I would have expected the notation to be: std::cout << "foo"; char foo << std::cin; I'm sure there's a good reason it is std::cin >> foo. What is it?
jds
  • 7,910
  • 11
  • 63
  • 101
23
votes
3 answers

Why is the use of cin, cout or %I64d preferred over %lld specifier in C++?

Why do many of the online judges advise "do not use the %lld specifier to read or write 64-bit integers in С++"? Is it preferred to use the cin, cout streams or the %I64d specifier?
Rishabh
  • 730
  • 2
  • 11
  • 25
23
votes
3 answers

Is it possible to read from a url into a System.IO.Stream object?

I am attempting to read from a url into a System.IO.Stream object. I tried to use Dim stream as Stream = New FileStream(msgURL, FileMode.Open) but I get an error that URI formats are not supported with FileStream objects. Is there some method I…
swolff1978
  • 1,845
  • 7
  • 28
  • 44
23
votes
2 answers

C++ Boolean evaluation

So I'm curious as to why this happens. int main() { bool answer = true; while(answer) { cout << "\nInput?\n"; cin >> answer; } return 0; } Expected behavior: 0 - Exits program, 1 - Prompts again, Any non-zero integer other…
user611105
22
votes
1 answer

Is it valid to construct an `std::ostream` from a null buffer?

Consider the following: std::ostream out(nullptr); Is this legal and well-defined? How about if I now do: out << "hello world\n"; Is this legal and well-defined? If so, presumably it's a no-op of sorts?
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
22
votes
2 answers

Infix vs prefix syntax: name lookup differences

Operators in C++ are usually considered to be an alternative syntax for functions/methods, especially in the context of overloading. If so, the two expressions below should be synonymous: std::cout << 42; operator<<(std::cout, 42); In practise, the…
Krzysztof Abramowicz
  • 1,556
  • 1
  • 12
  • 30
22
votes
3 answers

std::vector : cannot bind 'std::ostream {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&'

I encountered a confusing error message when trying to do something as simple as std::cout << std::vector{1,2,3}; which says cannot bind 'std::ostream {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&' int main() { …
thor
  • 21,418
  • 31
  • 87
  • 173
22
votes
3 answers

How can I compose output streams, so output goes multiple places at once?

I'd like to compose two (or more) streams into one. My goal is that any output directed to cout, cerr, and clog also be outputted into a file, along with the original stream. (For when things are logged to the console, for example. After closing,…
GManNickG
  • 494,350
  • 52
  • 494
  • 543
21
votes
2 answers

How is std::iostream buffered?

General use case I am trying to implement a basic shell. Description I need to read user input until some delimiters are pressed so a corresponding action can be performed. Those delimiter could be a single 'a', a single 'b' or a single 'c'. An…
authchir
  • 1,605
  • 14
  • 26
21
votes
3 answers

Hide user input on password prompt

Possible Duplicate: Read a password from std::cin I don't work normally with the console, so my question is maybe very easy to answer or impossible to do . Is it possible to "decouple" cin and cout, so that what I type into the console doesn't…
dom0
  • 7,356
  • 3
  • 28
  • 50
21
votes
2 answers

Can I use CreateFile, but force the handle into a std::ofstream?

Is there any way to take advantage of the file creation flags in the Win32 API such as FILE_FLAG_DELETE_ON_CLOSE or FILE_FLAG_WRITE_THROUGH as described here http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx , but then force that handle…
Steve Folly
  • 8,327
  • 9
  • 52
  • 63
21
votes
7 answers

Does anyone actually use stream extraction operators?

I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful. I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for…
Dan
  • 5,929
  • 6
  • 42
  • 52
21
votes
6 answers

Reading directly from an std::istream into an std::string

Is there anyway to read a known number of bytes, directly into an std::string, without creating a temporary buffer to do so? eg currently I can do it by boost::uint16_t len; is.read((char*)&len, 2); char *tmpStr = new char[len]; is.read(tmpStr,…
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
21
votes
3 answers

What is the difference between ios::app and ios::ate?

Possible Duplicate: C++ Filehandling: Difference between ios:app and ios:ate? What is the difference between these two file opening modes ? ios:ate sets the get/put pointer position to the end of the file so reading/writing will begin from end,…
Arun
  • 3,138
  • 4
  • 30
  • 41