读文件的一行
int main()
{
ifstream infile;
string filename;
cout << "Please enter the file name: ";
cin >> filename;
infile.open(filename.c_str());
string line;
vector<string> wordsOfFile;
while (getline(infile, line, '/n'))
{
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of(' ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfFile.push_back(word);
}
wordsOfFile.push_back(line.substr(prev_pos, pos - prev_pos));
}
infile.close();
size_t numOfLine = wordsOfFile.size();
cout << numOfLine << "words" << endl;
return 0;
}
int main()
{
ifstream infile;
string filename;
cout << "Please enter the file name: ";
cin >> filename;
infile.open(filename.c_str());
string line;
getline(infile, line, '/n');
infile.close();
vector<string> wordsOfLine;
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of(' ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfLine.push_back(word);
}
wordsOfLine.push_back(line.substr(prev_pos, pos - prev_pos));
size_t numOfLine = wordsOfLine.size();
cout << numOfLine << "words" << endl;
}
int main()
{
ifstream infile;
string filename;
cout << "Please enter the file name: ";
cin >> filename;
infile.open(filename.c_str());
string line;
vector<string> wordsOfFile;
while (getline(infile, line, '/n'))
{
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of(' ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfFile.push_back(word);
}
wordsOfFile.push_back(line.substr(prev_pos, pos - prev_pos));
}
infile.close();
size_t numOfLine = wordsOfFile.size();
cout << numOfLine << "words" << endl;
return 0;
}