几种获取文件有多少行的方法
使用C++中的ifstream 与 getline函数搭配使用 如:
std::string file_name = "F:\\phone_num_10000000.txt";
std::ifstream ifs(file_name.c_str());
int line_count = 0; ///记录文件中行的数量
std::string tmp_content; ///临时保存行数据
while (!ifs.eof())
{
std::getline(ifs, tmp_content, '\n');
///过滤空行
line_count += !tmp_content.empty();
}
std::cout << "line_count = " << line_count << std::endl;
以上想法是最先想到的,运行上面的程序,使用clock()函数计数,处理10000000行条数据大概用了3500左右的CPU时钟周期