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

fatal error: iostream: No such file or directory 3

A number of answers to this exact error have been put upon this website but I am quite the beginner to C++ and Code::Block so i'm afraid I do not understand them. I have been following a very simple C++ tutorial that started me out with one simple…
1
vote
0 answers

boost mapped_file_source exception

I use boost latest version 1.6.0 and Visual studio 2015. mapped_file_source throw exception when file size bigger than 500MB. the exception said "failed mapping view: The parameter is incorrect. : iostream stream error" How can i open bigger than…
user3451580
  • 31
  • 1
  • 5
1
vote
3 answers

Why can't i use std:cin as an argument

learning c++ and can't understand why I can't use "std::cin" as an argument. #include #include "stdafx.h" int doubleNumber(int a) { return 2 * a; } int main() { int x; std::cout << doubleNumber(std::cin >> x); return…
1
vote
0 answers

Inheriting from std::iostream, why has it been implemented like this?

By looking in the standard library documentation, it seems that for subclassing iostream, there are no other choice than implementing a streambuf class by overloading overflow and underflow. In some cases, it is a good idea. But when you want to…
galinette
  • 8,896
  • 2
  • 36
  • 87
1
vote
3 answers

In C++ std:: streams, after a failure, how to get a failure reason? Required: threadsafe and common to Windows and Linux (or at least Msvc/Gcc)

Sorry for weird title. Limited to 150 chars so couldn't use proper sentences. So let's say I've done the following to find out that something went wrong with my file stream: std::ofstream ofs; do_stuff_with(ofs); // streams don't throw on error…
JamEnergy
  • 720
  • 8
  • 21
1
vote
4 answers

WriteLog( ... ) function in C++

I'm trying to find a decent way to do logging from C++. My current solution is this: ostream & GetLog() { if( output == NULL ) throw error; return *output; } Where output is defined somewhere and can be a file or whatever. This is fine, but it…
Scott
  • 1,176
  • 2
  • 13
  • 19
1
vote
2 answers

is sequence necessary between cin.clear() and cin.ignore()?

Let me show you my source at first. #include #include using namespace std; int main() { int n; while (true) { cout << "Type >> "; cin >> n; if (cin.fail()) { cin.clear(); …
1
vote
1 answer

Fstream doesnt save the last word in the file and it doesnt read from the file, too

This a simple code for registering games in a file, but there are two problems. First, the parameter or attribute "clasification" is not being saved in the file. And second, when I read from the file, the content isn't shown. Before commenting, you…
Drako
  • 73
  • 8
1
vote
1 answer

how does the Cin and Cout objects work in a loop

I have this piece of code int main() { char ch; while (cin >> ch) cout << ch; return 0; } What I'm wandering is how does cin work in the while() loop? I mean, does it have an inner index to were it left off?
AnotherOne
  • 854
  • 2
  • 8
  • 19
1
vote
3 answers

Accepting extended ASCII codes as input in C++

My program is NOT accepting symbols such as "à" considering some city names in the world have these as characters. I'm using C++. I've tried all sort of streams of input such as getline(cin,string), fgets and such. All these forms in gathering input…
Robolisk
  • 1,682
  • 5
  • 22
  • 49
1
vote
1 answer

creating input stream manipulator

As an exercise, I'm trying to create a input stream manipulator that will suck up characters and put them in a string until it encounters a specific character or until it reaches eof. The idea came from Bruce Eckel's 'Thinking in c++' page…
Mike D
  • 2,753
  • 8
  • 44
  • 77
1
vote
3 answers

std::ifstream::open() not working

I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini file so that the game designers can tweak the game parameters without requiring help from me in addition to a re-compile. This is what I'm doing…
Aistina
  • 12,435
  • 13
  • 69
  • 89
1
vote
1 answer

Pseudo-istream pointer return

I've been going through Stroustrup's Programming and Principles to teach myself c++11. In chapter 11, he describes a program that removes (turns into whitespace) any un-wanted characters from an input stream. So, for example, I could set a string…
1
vote
2 answers

Stream Manipulation for outputting object data in different formats

Say I have an employee object with the following data members: class Employee { private: int _id; std::string _name; std::string _address; std::string _city; std::string _state; std::string _country; std::string _phone; …
1
vote
1 answer

Allowing User Input with Whitespace?

I'm trying to get user input for a function. The following code works, but not when there is a space between words. string cityNew; string cityPrevious; cout<<"Enter a city name:"<>cityNew; cout<<"Enter a previous city…
Σqu
  • 47
  • 7