Skip to content

I/O

See I/O example

See File stream example.

cout#

Standard C++ output device (default: console).

1
cout << "Some text" << endl;

cin#

Standard C++ input device (default: keyboard).

1
cin >> var;

endl#

End of line.

1
cout << "Some text" << endl;

ifstream#

Open a file in read mode.

1
ifstream file("file.txt");

Note

A file is closed at the end of a block.

ofstream#

Open a file in write mode.

1
ofstream file("file.txt");

Note

A file is closed at the end of a block.

append#

Append data to an existing file.

1
ofstream file("file.txt", append);

binary#

Write a file in binary.

1
ofstream file("file.btxt", binary);

seekg#

Set the file position.

1
file.seekg(Pos);

tellg#

Get the file position.

1
int Pos = file.tellg();

flush#

Flush the buffer of the file.

1
file.flush

getline#

Get the current line.

1
2
string s;
getline(file, s);

Output format#

In the descriptions below, f is an output stream, for example cout or a ofstream.

All this methods, excepted the first, return a stream, so they can be chained:

1
cout.scientific.showpos << 3 << endl;

precision#

Set the number of digits printed to the right of the decimal point. This applies to all subsequent floating point numbers written to that output stream. However, this won't make floating-point "integers" print with a decimal point. It's necessary to use fixed for that effect.

1
int np = f.precision(n)

scientific#

Formats floating-point numbers in scientific notation

1
f.scientific

fixed#

Used fixed point notation for floating-point numbers. Opposite of scientific.

1
f.fixed

showbase#

Converts insertions to an external form that can be read according to the C++ lexical conventions for integral constants. By default, showbase is not set.

1
f.showbase

noshowbase#

Unset showbase flags.

1
f.noshowbase

showpos#

Inserts a plus sign (+) into a decimal conversion of a positive integral value.

1
f.showpos

noshowpos#

Unset showpos flags.

1
f.noshowpos

default#

Reset all the previous flags to the default expect precision.

1
f.default

setw#

Behaves as if member width were called with n as argument on the stream on which it is inserted as a manipulator (it can be inserted on output streams).

1
f.setw(n)