8.1 8.2
#include<iostream>
#include<string>
using namespace std;
istream& fcn(istream& is)
{
string word;
while (is >> word && !is.eof())
{
cout << word << endl;
is.clear();
}
return is;
}
int main(int argc, char* argv[])
{
fcn(cin);
return 0;
}
8.3
输入类型不合法;文件结束
8.4
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
string file = "8-4.txt";
vector<string> str;
ifstream in(file);
if (in)
{
string word;
while (getline(in, word))
str.push_back(word);
}
else
cerr << "couldn't open this file: " << endl;
for (auto it = str.begin(); it != str.end(); ++it)
cout << *it << endl;
return 0;
}
8.5
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
string file = "8-4.txt";
vector<string> str;
ifstream in(file);
if (in)
{
string word;
while (in >> word)
str.push_back(word);
}
else
cerr << "couldn't open this file: " << endl;
for (auto it = str.begin(); it != str.end(); ++it)
cout << *it << endl;
return 0;
}