<fstream>
Defines types ifstream and ofstream representing input and output files respectively. ifstream is derived from istream, inheriting all its operations (such as >>). In addition, ifstream in(cp); // Open file named cp for reading
ifstream in(cp, ios::in | ios::binary); // Open in binary mode
bool(in); // true if open successful
cp is the file name. It must be a char*, not string (use s.c_str() to convert string s). Input is normally in text mode. In Windows, carriage returns ('\r') are discarded, and an ASCII 26 ('\032') signals end of file. In binary mode and in UNIX, no such translation occurs. The file is closed when the ifstream is destroyed.
{
ifstream f("input.dat", ios::in | ios::binary);
if (!f)
cerr << "File not found\n";
else {
int i=f.get(); // First byte or EOF if empty
}
} // f closed here
ofstream is derived from ostream, inheriting all its operations (such as <<). In addition,
ofstream os(cp); // Open file named cp for writing
ofstream os(cp, ios::out | ios::binary); // Open in binary mode
In text mode in Windows, writing '\n' actually writes "\r\n". The file named cp is overwritten if it exists, or created otherwise. The file is flushed and closed when the ofstream is destroyed.
Defines types ifstream and ofstream representing input and output files respectively. ifstream is derived from istream, inheriting all its operations (such as >>). In addition, ifstream in(cp); // Open file named cp for reading
ifstream in(cp, ios::in | ios::binary); // Open in binary mode
bool(in); // true if open successful
cp is the file name. It must be a char*, not string (use s.c_str() to convert string s). Input is normally in text mode. In Windows, carriage returns ('\r') are discarded, and an ASCII 26 ('\032') signals end of file. In binary mode and in UNIX, no such translation occurs. The file is closed when the ifstream is destroyed.
{
ifstream f("input.dat", ios::in | ios::binary);
if (!f)
cerr << "File not found\n";
else {
int i=f.get(); // First byte or EOF if empty
}
} // f closed here
ofstream is derived from ostream, inheriting all its operations (such as <<). In addition,
ofstream os(cp); // Open file named cp for writing
ofstream os(cp, ios::out | ios::binary); // Open in binary mode
In text mode in Windows, writing '\n' actually writes "\r\n". The file named cp is overwritten if it exists, or created otherwise. The file is flushed and closed when the ofstream is destroyed.
No comments:
Post a Comment