#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
const string FILENAME = "words.txt"; // 文件名
// 打开文件输出流,输出数据
ofstream out(FILENAME);
out << "This is far too long to put on a "
"single line but it can be broken up with "
"no ill effects\nas long as there is no "
"punctuation seperating adjacent character "
"arrays.\n";
out.flush(); // 清空缓冲区
out.close(); // 关闭输出流
// 打开文件输入流,按空格读取数据
ifstream in(FILENAME);
size_t wordCount = 0; // 单词数
for (string word; in >> word;) {
++wordCount;
}
cout << "Word count is " << wordCount << endl;
return 0;
}
C++、统计文件单词数
最新推荐文章于 2024-07-17 20:43:45 发布