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

Difficulty overloading operator<< for file handling class

I have to: Define a File_handle class with constructor that takes a string argument (file name), opens the file in the constructor, and closes it in the destructor. As I understand it, this class is used to provide RAII and I am trying to…
Ziezi
  • 6,375
  • 3
  • 39
  • 49
1
vote
1 answer

wifstream.tellg() advancing position in the stream?

I've been doing some basic file manipulations with iostreams in Visual Studio 2015 and noticed this weird behavior. #include #include #include void main(int argc, char** argv) { std::wifstream…
unlink
  • 15
  • 3
1
vote
1 answer

why would cin.fail() pass input=100a if 'input' is an int?

int main(void) { int valid_input(); int ll_input=0; int resp; hash_table obj; int bucket; while(1) { ll_input=valid_input(); if(ll_input>10) { break; } obj.insert(ll_input); } …
Anurag
  • 651
  • 4
  • 18
1
vote
4 answers

Redirecting standard output to integer

I was solving the following problem: Reverse the digits of an integer. Now the code for the problem is straightforward. void reverse(int x){ if(x < 0){ cout << "-"; x*=(-1); } while(x!=0){ cout << x%10; x/=10; …
Akshay Arora
  • 729
  • 1
  • 8
  • 20
1
vote
1 answer

porting ostream::opfx / osfx from Unix to Linux

I am porting some C++ code from Unix to Linux (Red Hat). I have run into the following pattern: ostream& myfunction(ostream& os) { if (os.opfx()) { os << mydata; os.osfx(); } return os; } The functions opfx and osfx are not…
Bill
  • 14,257
  • 4
  • 43
  • 55
1
vote
2 answers

c++ output figuring out problems

#include using namespace std; void f(int& p) { p += 2; } int main() { int x = 10; f(x); int y = x + 1; f(y); cout << "x is " << x << endl; cout << "y is " << y << endl; system("PAUSE"); return…
Nina555
  • 15
  • 6
1
vote
4 answers

What happens when an ofstream object.open() is commented out?

#include #include #include #include using namespace std; int main(){ ofstream out; ifstream in; out.open("The Necessary Death of Charlie Countryman2.srt"); if (out.fail()) { …
nSv23
  • 429
  • 6
  • 19
1
vote
2 answers

std::cin recieving input when it shouldn't

I've been having an issue recently with std::cin where when I try to use it in conjunction with std::this_thread::sleep_for(), it starts to get an input when it hasn't even been called yet. Here's the code I've been using: #include…
Paul T.
  • 31
  • 7
1
vote
1 answer

C++ STL streambuf exception handling

Here I have this streambuf, ostream structure (modified from here http://wordaligned.org/articles/cpp-streambufs), where I try to throw from two points in the code. But I never can catch those exceptions in the main() and the program exits normally.…
hovnatan
  • 1,331
  • 10
  • 23
1
vote
1 answer

File stream with inputted file name

I want to prompt the user for the file name then use that name to open up a file stream. I think I need to make it into a const char or something but I dont really know what that is or how to do so. cout << "Enter Locations Filename: "<>…
Matt
  • 89
  • 7
1
vote
1 answer

Parsing CSV with empty values using C++ iostreams

I am having trouble correctly parsing a CSV file. Some of the values in the data rows can be blank, and my code does not work correctly when I have blank entries in any of the value rows. Without blank entries, the program returns the following…
johnco3
  • 2,401
  • 4
  • 35
  • 67
1
vote
1 answer

Can the state of a standard C++ iostream manipulator be polled?

When writing an overload for the iostream << operator, particularly on an application class, it may be desirable to alter the behavior of that overload based on the standard manipulators in effect on that stream object. Can the state of standard…
Ken Clement
  • 748
  • 4
  • 13
1
vote
2 answers

How to set output to middle?

Is it possible to write code to make my outputted text formatted to the middle of the screen? I have tried a lot, but nothing has worked. Here is what I have thought of so far. cout.setf (ios::middle); That was an error. Also I…
Andrew Tew
  • 47
  • 1
  • 8
1
vote
1 answer

C++ removing non-alphabetic chars from beginning of a word

I have the following code in a method of string type which reads words from a file, removes all non-alphabetic chars (as shown below) from the beginning of the word and returns them after it does so. After printing out the results in the main…
1
vote
5 answers

C++ Is there any way to make a program run faster?

For example.. if I had. #include using namespace std; int main() { int counter = 0; while (true) { cout << counter << endl; counter++ } } And say I was on a race to counting to 1 billion against other computers,…
Tommy Saechao
  • 1,099
  • 4
  • 17
  • 28