This chapter begins by giving you more insight into the features provided by the << and >> operators. It then moves on to introduce the notion of data files and shows you how to implement file-processing applications.
<< >>用于窗口的输入输出。
包含的头文件是istream 和ostream
还有文件的输入输出,包含的头文件是ifstream 和ofstream
下面举一个例子,表示文件的操作。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string promptUserForFile(ifstream &infile, string prompt = "")
{
while (true) {
cout << prompt;
string filename;
getline(cin, filename);
infile.open(filename.c_str());
if (!infile.fail()) return filename;
infile.clear();
cout << "Unable to open that file. Try again." << endl;
if (prompt == "") prompt = "Input file: ";
}
}
int ma