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
3 answers

How do tellg() & tellp() work in C++?

Why do tellg() and tellp() always come together? And how can we separate them from each other?
mahroo
  • 57
  • 1
  • 6
1
vote
1 answer

error: no match for operator<< in std::operator<< >(*&std::cout),((const char*)

error: no match for operator<< in std::operator<< >(*&std::cout),((const char*) #include using namespace std; int ContarDig (int num){ int contar=1; while (num>9){ num=num/10; …
user3553038
  • 13
  • 1
  • 4
1
vote
2 answers

array of ifstream objects in C++

I am trying to create an array of ifstream objects, the code compiles and I am able to create the array of ifstream objects of size sizeargs-1 however once I try to open a file in one of the ifstream objects the program crashes, it's very…
user3545370
  • 205
  • 1
  • 4
  • 16
1
vote
1 answer

Why just including iostream.h makes executable weigh 1mb more?

It is not even a "hello world", it is simply: #include int main() { return 0; } which weighs 1080 kb. When I remove the iostream inclusion in the program to get int main() { return 0; } the executable's size becomes only 49…
user3496846
  • 1,627
  • 3
  • 16
  • 28
1
vote
2 answers

in c++, is it possible to / how can i display console output after input, on same line, before user's input is given?

In C++, when writing to and taking information from the console using cout / cin, is it possible to do something like ENTER YOUR DATA HERE --> __ <-- ENTER YOUR DATA HERE With the user input cursor where the underscores are located, and output…
1
vote
1 answer

Left and right justifying output consisently

My project is to create this output using setw(), setfill(), setiosflags(): Enter KWH used 993 C O M P S C I Electric ------------------------------------------------ Kilowatts Used …
ashraj98
  • 384
  • 5
  • 17
1
vote
1 answer

AOSP : Unkown C++ headers ( no such file or directory for , , .. >

I've downloaded the Android Tree and succeeded to compile an run it on a device. After that, I added some C++ files and I got error like: no such file or directory. I tried to add the libstlport_static but that did not work for me, I think I…
Cherry'salma
  • 65
  • 1
  • 5
1
vote
3 answers

Relating efficiency of fgets() function and using >> operator

I was developing programs for reading data from files in C++. I came up with two different methods to do it. But am not sure which is efficient and i dont even know how to find the efficiencies of a program. I have given yu two snippets. Please…
1
vote
0 answers

making a gradebook that reads in from ifstream

I have to take input from a text file that is in the following format: Attendance: 5 Midterm: 20 Final: 20 Homework: 15 Projects: 40 Henry, Patrick Attendance: 12 15 Midterm: 80 100 Homework: 50 100 Homework: 60 100 Homework: 80 100 Project: 90…
chris1995
  • 13
  • 5
1
vote
2 answers

Making a program stop till enter is pressed

I have a programme that when the command (cin) help is entered it brings up a help note. That note looks somewhat like this: C++ if (cmd == "help") { cout << "▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄\n"; cout << "██ Help Menu …
abaft
  • 152
  • 2
  • 12
1
vote
3 answers

C# Check EOF in text file to fix array

I have a code to read text file and save the data into a double array to plot graph: string filename = openFileDialog1.FileName; var lineCount = 0; using (var reader = File.OpenText(@filename)) { string line; while ((line =…
Ren
  • 765
  • 4
  • 15
  • 42
1
vote
3 answers

Input streaming for user defined class

For a user defined class I have overloaded << operator in the following way for cout ostream& operator<<(ostream& os, const myObject& obj_) { if (obj_.somefloat != 0) os << "(" << obj_.somefloat << ")"; else if ( obj_.oneint != 0 &&…
eldos
  • 3,132
  • 3
  • 29
  • 32
1
vote
1 answer

C++ multiple cin.get()

Could seem an old question, but the problem here isn't the use of TWO cin.get(), but of more than two! if I write (in DEV C++) I get just one input request (s) and then end program. Now, I expected having at least two request of cin, because I…
volperossa
  • 1,339
  • 20
  • 33
1
vote
2 answers

Read float from input stream without trailing "E"

I do the following: float f; cin >> f; On the string: 0.123W Number 0.123 will be properly read to f, and stream reading will be stopped on 'W'. But if we enter: 0.123E operation will fail and cin.fail() will return true. Probably the trailing…
peper0
  • 3,111
  • 23
  • 35
1
vote
1 answer

Unable to read from text file and store into string

I have a text file 0 Po Tom Mr 123AlphabetStreet Netherlands Wulu 123456 D01 Malaysia SmallAdventure 231112 0 Liu Jack Mr 123AlphabetStreet Italy Spain 123456 D02 Afghanistan TriersAdventure 030214 I am trying to read the txt file:Form.txt, store…
Computernerd
  • 7,378
  • 18
  • 66
  • 95