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

Sorting files in a directory by date in c# using directory.get files()

At the moment I have my code to get some files from a Dir. foreach (var file in Directory.GetFiles(MainForm.DIRECTORY_PATH, "*.csv")) { //Process File string[] values = File.ReadAllLines(file) …
Jordan Atkinson
  • 202
  • 1
  • 6
  • 24
1
vote
2 answers

"The process cannot access the file because it is being used by another process "

I'm writing my application in C# using Windows Form. I'd like to execute CMD command, save result to textfile and then open this file in my program to parse and finally use values what I need. Unfortunately, when I run it CMD write "The process…
Robert
  • 140
  • 1
  • 8
1
vote
1 answer

how to write any custom data type to file using ifstream?

as question says, i want to write custom data type data of a class maybe to a file using ifstream in c++. Need help.
NativeCoder
  • 1,033
  • 1
  • 10
  • 16
1
vote
4 answers

C++ cout with prefix

I want a ostream with a prefix at the beginning of every line redirected on cout; I try this: #include #include class parallel_cout : public std::ostream { public: parallel_cout(std::ostream& o):out(o){} template
volperossa
  • 1,339
  • 20
  • 33
1
vote
2 answers

Output a C array through ostream

I'm trying to output C array using iostream. For array of ints, I wrote the code like this template ostream& operator<< (ostream& os, const int (&x)[N]) { for(int i=0; i
Junjie
  • 469
  • 1
  • 4
  • 14
1
vote
1 answer

How to fix "Assertion '__pos <= size' failed" error?

I'm trying to write a function that compares two strings, say, s1 and s2, and if at some position, s1[i] == s2[i], then it must increase a counter by one (i.e. it counts the number of cases which at the same position (say, i), they contain the same…
aleixrr
  • 461
  • 4
  • 18
1
vote
2 answers

no appropriate default constructor available initialising ostream pointer

I am trying to create a logging class like this: #include #include class logstream : public std::ostream { public: logstream() : os(&std::cout), file_(false) {} // line 7 logstream(const char* filename) : os(new…
Angus Comber
  • 9,316
  • 14
  • 59
  • 107
1
vote
1 answer

Pstreams - how to write to stdin and get the output?

I'm looking for a working example of how to use pstreams. I'm planning to open an ssh session with a remote host, execute a command and receive its output. So far I got this: #include #include #include int…
Tomasz Kasperczyk
  • 1,991
  • 3
  • 22
  • 43
1
vote
3 answers

fatal error C1075: end of file found before the left brace and read and write files not working

could someone also tell me if the actions i am attempting to do which are stated in the comments are correct or not. i am new at c++ and i think its correct but i have…
user320950
  • 187
  • 3
  • 7
  • 15
1
vote
1 answer

error no instance of overloaded function "getline" matches the argument list c++

error no instance of overloaded function "getline" matches the argument list I cant seem to see what is wrong. I feel like I am passing the correct arguments (ie the std::ofstream and the std::string). Any help would be great thanks. #include…
1
vote
2 answers

What kind of exceptions can formatted output operators standard iostream objects throw?

(1) Given an arbitrary variable x of a fundamental (possibly CV-qualified) type X, and (2) a formatted output statement in the form of outputStream << x; where outputStream is any of the standard output streams (cout, cerr, clog, wcout,…
jotik
  • 17,044
  • 13
  • 58
  • 123
1
vote
2 answers

Reading a text file from the first line multiple times (C++)

I'm using "getline" to read some lines in a text file. It works as it should, but I'm calling the method multiple times. while(getline(file, line)) { //Do something } //More code in between while(getline(file, line)) { //Do something…
Ben
  • 1,299
  • 3
  • 17
  • 37
1
vote
0 answers

cin, getline, leading whitespace: ignore vs ws

So every once in a while, when I read documentation for getline, I get reminded of the infamous whitespace issue. Yes, I know what it is. This question is about the supposedly standard…
stan423321
  • 183
  • 6
1
vote
1 answer

where are the definitions for the cout, cin objects placed?

Are they in the namespace std? or in the header file ? I have heard that only contains the prototype of functions and not the definitions. Am I right?
Manoj Bharadwaj
  • 191
  • 1
  • 1
  • 10
1
vote
2 answers

loop to discard spurious variable input from stdin, Linux C++

I am trying to write a c++ program for my linux machine that can interact with some instrumentation that responds to simple ascii commands. The problem I'm running into, I would think, would be a fairly common request but my searches of various…
Justin Frahm
  • 193
  • 9