1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 5 int main() { 6 std::ifstream inputFile("D://article.txt"); 7 8 if (!inputFile) { 9 std::cout << "无法打开输入文件!" << std::endl; 10 return 1; 11 } 12 13 int wordCount = 0; 14 int lineCount = 0; 15 std::string line; 16 17 while (std::getline(inputFile, line)) { 18 lineCount++; 19 20 21 size_t pos = 0; 22 while ((pos = line.find_first_not_of(" \t", pos)) != std::string::npos) { 23 pos = line.find_first_of(" \t", pos + 1); 24 wordCount++; 25 } 26 } 27 28 inputFile.close(); 29 30 std::cout << "文章中单词个数:" << wordCount << std::endl; 31 std::cout << "文章中行数:" << lineCount << std::endl; 32 33 return 0; 34 }
标签:std,ifstream,int,5.18,include,inputFile From: https://www.cnblogs.com/lijianlongCode13/p/17413641.html