如何计算文本文件中的行数。 我试过以下代码,但输出始终为 0。 用户输入他/她要读入的.txt文件,然后程序会计算文件中的行数。
编辑:
ifstream in (infile.c_str(), ios::in);
if(!in.is_open())
{
cout << "Error - opening file: " << infile << endl;
return;
}
while(!in.eof())
{
in >> type;
if(in.fail()) break;
//read corresponding object
if(type == "Point2D,")
{
in >> p2d;
//add to container
points2d.push_back(p2d);
}
else if(type == "Point3D,")
{
in >> p3d;
//add to container
points3d.push_back(p3d);
}
else if(type == "Line2D,")
{
in >> line2d;
//add to container
lines2d.push_back(line2d);
}
else if(type == "Line3D,")
{
in >> line3d;
//add to container
lines3D.push_back(line3d);
}
}
in.close();
while (getline(in, line))
++noOfRec;
cout << noOfRec<<" records reading successfully!" << endl;
//return 0;
最佳答案
in.close();
您已经关闭了文件。 您应该再次打开它或使用 seekg() 将获取指针放回文件开头
关于c++ - 计算文本文件C++中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20028326/