gpt4 book ai didi

c++ - 计算文本文件C++中的行数

转载 作者:行者123 更新时间:2023-11-28 03:04:39 25 4
gpt4 key购买 nike

如何计算文本文件中的行数。我试过以下代码,但输出始终为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com