C++ 行数始终为0
为什么行数总是0?它应该是10,但是输出总是0。这个方法有什么问题吗C++ 行数始终为0,c++,getline,C++,Getline,为什么行数总是0?它应该是10,但是输出总是0。这个方法有什么问题吗 int main() { vector<double> doubleCoefficient; // vector to store unknown number of equations (double) /* Read from file and assign values to vector */ //File stream object ifstream inputFile
int main() {
vector<double> doubleCoefficient; // vector to store unknown number of equations (double)
/* Read from file and assign values to vector */
//File stream object
ifstream inputFile;
//Open the txt file
inputFile.open("test.txt");
//search for the text file
if(!inputFile.is_open())
{
cerr << "Error opening file \n";
exit(EXIT_FAILURE);
}
else
{
cout << "File found and successfully opened. \n";
}
double x;
while(!inputFile.eof()){
inputFile >> x;
doubleCoefficient.push_back(x);
}
int count =0;
string line;
while (getline(inputFile, line)){
count++;
}
cout << "Number of lines in text file:" << count << endl;
inputFile.close();
}
还有一段时间!如果您转到文件的末尾,那么在此之后,您将无法读取行。
你需要回到开始使用
试一试
在计算行数之前。这是因为您从文件末尾开始计算,所以不会计算任何内容。
fseek ( inputFile , 0 , SEEK_SET );