为什么行数总是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;
//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();
}
答案 0 :(得分:1)
使用while(!inputFile.eof())
您将转到文件的末尾,因此,您无法读取行。
您需要使用fseek()
尝试
fseek ( inputFile , 0 , SEEK_SET );
在计算线之前。