char
and wchar_t
ifstream
- file streams that read files
ofstream
- file streams that write files
fstream
- file streams that read and write files
<fstream>
include file
open()
connects a file stream to a
file
void open(const char fname[])
close()
breaks the connection between a
file stream and a file
void close(void)
void read(char data[], int data_size)
data_size
bytes from the file stream into data
void write(const char data[], int data_size)
data_size
bytes from data
into the file stream
bool eof(void)
stream member function returns true at eof,
false otherwise
while (!inf.eof()) inf.read(data, data_size);is wrong
while (true) { inf.read(data, data_size); if (inf.eof()) break; }is right
int gcount(void)
returns the number
of bytes last read
outs << value
outs
is an io stream opened for writing
value
is a value of a type the insertion operator knows how to
convert (int
, char
, string
and so on)
value
is converted into characters
ins >> var
ins
is an io stream opened for reading
var
is a variable having a type the extraction operator knows
how to convert
'0'
through
'9'
possibly proceeded by either '-'
or
'+'
; all the digits are read, but not necessarily used
var
cout << "i = "
<< i << "\n"
cin
hits eof, it returns NULL
- while (cin >> i)
eof
- the end-of-file condition has been detected
fail
- non-fatal i-o error, usually a format error
bad
- a fatal error; undefined state
bool eof(void)
, and so on
bool good(void)
- true if no errors; false otherwise
ifstream inf; const string fname = "my.dat"; inf.open(fname.c_str()); if (!inf.good()) cerr << "Can't open " << fname << "\n";
inf.read(data, data_size); if (inf.eof()) // whatever if (!inf.good()) // whatever
bool operator!(stream)
returns true if there are no errors
operator!
doesn't consider eof an error
ifstream inf; const string fname = "my.dat"; inf.open(fname.c_str()); if (!inf) cerr << "Can't open " << fname << "\n";
while (cin >> x) { ... }
void *
pointer.
good()
over operator!
fail
error
var
does not receive a value
good()
returns false
open()
describes how the file should be
opened
ios::in
- open for reading, file pointer at the beginning of the
file
ios::out
- open for writing, file pointer at the beginning of
the file; this trashes the previous file contents
ios::app
- open for writing (appending), file pointer at the end
of the file; this preserves the previous contents of the file
ifstream
s is ios::in
ofstream
s is ios::out
- the default open
for writes is destructive
fstream
s have no defaults - you must specify ios::in
,
ios::out
, or ios::in + ios::out
open()
ifstream inf("my.dat");
void put(char)
file stream member function
int get(void)
file stream member function
istream getline(istream, string, char)
function
<string>
, not in the stream includes
<iomanip>
hex
, dec
, oct
int i = 135 ; cout << i << " in hex is " << hex << i << "\n"
cout << "Input a hex number: " ; cin >> hex >> i
setbase(i)
manipulator where i = 8, 10, or 16
setw(i)
i
characters to format
cout << setw(5) << 3
produces " 3"
cout << setw(1) << 123
produces "123"
setw()
is not sticky
cout << setw(4) << "a" << "b"
produces " ab"
cout << setw(4) << "a" << setw(4) << "b"
produces " a b"
setfill(c)
manipulator - use character c
as the pad
cout << setw(5) << setfill('*') << 1
produces "****1"
setfill()
is sticky
setiosflags()
manipulator - modify control bits
ios::left
- left justify output; "1 "
ios::right
- right justify output; " 1"
ios::showpoint
- output floating point with a dot and zeros
ios::skipws
- skip whitespace on input; also the ws
manipulator; this usually doesn't do what you want
ios::scientific
- output floating point in scientific notation;
also(ios::fixed
ios::uppercase
- output uppercase hex digits; also ios::lowercase
resetiosflags()
manipulator
setw()
manipulator and the width()
stream member
function
setfill()
manipulator and the fill()
stream member
function
setiosflags()
manipulator and the setf()
stream member
function - also unsetf()
std::ios::pos_type tellg(void)
and std::ios::pos_type
tellp(void)
istream seekg(pos_type)
and ostream seekp(pos_type)
reposition the file pointer as indicated
istream seekg(off_type seek_dir)
and ostream seekp(off_type,
seek_dir)
reposition the file pointer relative to the indicated
position
std::ios::beg
, cur
, and end
.
This page last modified on 29 January 2002.