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

Writing in a txt file

In trying to read and write datas in a .txt file. Reading is working fine, but each time i try to write, program overwrite on what it wrote before. Only the last thing written remains. void trace(Board& tab) { ofstream …
Csi
  • 526
  • 3
  • 22
1
vote
2 answers

Unable to read two strings with cin.get()

Why does trying to input two strings using cin.get() fails? I can successfully read the first string but the input fails for second string and subsequent operations.. See the code: #include #include int main(){ long int…
anshabhi
  • 423
  • 6
  • 29
1
vote
6 answers

C++: Print only one char

When I read one char* from std::cin and I want to write it to std::cout, it prints until it finds a \0 in memory. So what did was: char c; cin >> c; char* pChar = &c; pChar++; *pChar = '\0'; println(&c); // My own method: void println(char * str) {…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1
vote
4 answers

How to debug segmentation fault?

It works when, in the loop, I set every element to 0 or to entry_count-1. It works when I set it up so that entry_count is small, and I write it by hand instead of by loop (sorted_order[0] = 0; sorted_order[1] = 1; ... etc). Please do not tell me…
user1994036
1
vote
2 answers

Faster way to read random number of integers from console C++

First I would like to say, that C++ is not my basic language, and I ask for forgiveness if it's basic knowledge. My problem is that I have to overcome spoj time limit with my algorithm. I am confident that algorithm itself is good (and I dont't…
1
vote
1 answer

Issue with iostream_iterator with tuple C++

I want to use iostream_iterator for tuple. I have overload input operator >> for the tuple. But compiler give compilation error. #include #include #include #include #include using namespace…
1
vote
1 answer

Ifstream.getline() - Only reading first line?

I'm just trying to run a simple c++ program that will format a .txt file with data entries. I have run it with many different text files of the exact same format, and now it just won't work. I'm sure the solution is simple. Here is a simplified…
Arturo don Juan
  • 249
  • 2
  • 8
1
vote
2 answers

How to store data from a file read in binary mode C++

Hi i am trying to read a file , say for example 'sample.txt', in binary mode-c++ and i need to store the file text (eg."nodeA nodeB") in a vector . eg: "A A B E A B G" if this is what is in the text file , i want to read it in binary form and then…
1
vote
2 answers

C++ Calling - search function

I was wondering how I could finish up this program. It's to perform a linear search on a list "ll" (which length is 31) for the user inputted item it, returning the user inputted numbers and their locations if they're found. Problem: I'm not sure…
csheroe
  • 87
  • 3
  • 11
1
vote
2 answers

cin infinite loop when reading in a non-numeric value

I had a strange behavior in a program and I spent long time trying to deduce why. it was an infinite loop with no sense. Testing these lines of code(under suspicion) i got the same result. Every time I type in a non-numeric value such a symbol, the…
user3477273
1
vote
1 answer

GCC, std::ctype specialisation & streams

I've written my own specialisation of each virtual member function of std::ctype, so that this now works: #include #include #include "char16_facets.h" // Header containing my ctype specialisation #include…
Tom
  • 7,269
  • 1
  • 42
  • 69
1
vote
1 answer

Connecting two streaming functions c++

I have two functions: Get (std::ostream* os) and Put (std::istream& is) The way get functions is, it has multiple streams underneath from which it gathers the data and sends it out via the output stream os. Put on the other hand, writes the data…
sethu
  • 1,691
  • 4
  • 22
  • 34
1
vote
1 answer

GCC 4.8 and char16_t streams - bug?

Is this a libstdc++ bug? #include #include using namespace std; int main() { basic_string str(u"0.0"); basic_stringstream sstr(str); double x = 9; sstr >> x; } Output, under…
Tom
  • 7,269
  • 1
  • 42
  • 69
1
vote
3 answers

std::cout formatting in Cpp 98 standard

Each of the following regards a distinctive ostream-format. How do I return it to default? #include int main() { std::cout << std::fixed; std::cout << std::setprecision(5) << f << '\n'; std::cout << "scientific:\n" <<…
1
vote
2 answers

Not getting the right output reading a file in c++

I'm helping a friend with a simple c++ task about reading a file and printing it, here is the code: #include #include #include using namespace std; const int P=10; struct persona { string nombre; int…
Franchojavilondo
  • 33
  • 1
  • 2
  • 8