I/O
See I/O example
See File stream example.
cout
Standard C++ output device (default: console).
1cout << "Some text" << endl;
cin
Standard C++ input device (default: keyboard).
1cin >> var;
endl
End of line.
1cout << "Some text" << endl;
ifstream
Open a file in read mode.
1ifstream file("file.txt");
Note
A file is closed at the end of a block.
ofstream
Open a file in write mode.
1ofstream file("file.txt");
Note
A file is closed at the end of a block.
append
Append data to an existing file.
1ofstream file("file.txt", append);
binary
Write a file in binary.
1ofstream file("file.btxt", binary);
seekg
Set the file position.
1file.seekg(Pos);
tellg
Get the file position.
1int Pos = file.tellg();
flush
Flush the buffer of the file.
1file.flush
getline
Get the current line.
1string s;
2getline(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:
1cout.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.
1int np = f.precision(n)
scientific
Formats floating-point numbers in scientific notation
1f.scientific
fixed
Used fixed point notation for floating-point numbers. Opposite of scientific.
1f.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.
1f.showbase
noshowbase
Unset showbase
flags.
1f.noshowbase
showpos
Inserts a plus sign (+) into a decimal conversion of a positive integral value.
1f.showpos
noshowpos
Unset showpos
flags.
1f.noshowpos
default
Reset all the previous flags to the default expect precision.
1f.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).
1f.setw(n)