May 2003
Intermediate to advanced
808 pages
32h 24m
English
ifstream class — Input file stream
typedef basic_ifstream<char> ifstream;The ifstream class is a
specialization of the basic_ifstream template for char characters. Example 13-9 shows a simple use
of the ifstream and ofstream classes.
#include <cstdlib>
#include <fstream>
#include <cstdio> // For perror( )
#include <iostream> // For cerr
int main(int argc, char** argv)
{
if (argc != 3) {
std::cerr << "usage: copy FROM TO\n";
return EXIT_FAILURE;
}
// Open the input file.
std::ifstream in(argv[1]);
if (! in) {
std::perror(argv[1]);
return EXIT_FAILURE;
}
// Open the output file.
std::ofstream out(argv[2]);
if (! out) {
std::perror(argv[2]);
return EXIT_FAILURE;
}
// Copy the input to the output, one character at a time.
char c;
while (in.get(c))
out.put(c);
out.close( );
// Make sure the output was written.
if (! out) {
std::perror(argv[2]);
return EXIT_FAILURE;
}
}basic_ifstream class
template, wifstream
class, istream in <istream>
Read now
Unlock full access