我编写了一个程序来从文件中读取实数, 该程序应该提供5个输出,因为有5个数字列表,但是我得到的是最后一个数字列表。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
// std::ios::sync_with_stdio(false); // to speed up reading
ifstream testfile;
string line;
uint_fast16_t n,i=0;
float lat1,lat2,lng1,lng2;
testfile.open("testfile.txt");
if (!testfile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
testfile.seekg(19,ios::beg);//skip to last of first line
testfile>>n;
testfile.seekg(21,ios::beg);
getline(testfile,line);//read next line;
while (!testfile.eof())
{getline(testfile,line,' ');
testfile>>lat1;testfile>>lng1;testfile>>lat2;testfile>>lng2;
cout<<" "<<lat1<<" "<<lng1<<" "<<lat2<<" "<<lng2<<endl;
}
testfile.close(); // Step 5: Closing file
return 0;
}
文本文件文件内容
`Number_Of_Location=5
Location'Number' Northwest_Latitude NorthEast_Longitude SouthEast_Latitude SouthEast_longitude
Location1 20.2783121 85.7684699 20.2722087 85.7817819
Location2 28.9763977 77.636981 28.973047 77.6388145
Location3 28.9756777 77.638032 28.9736137 77.642148
Location4 29.9827777 78.1222222 29.9755555 78.1325000
Location5 29.9788888 78.0630555 29.9608333 78.0841666
`
输出
20.2783 85.7685 20.2722 85.7818
28.9764 77.637 28.973 77.6388
28.9757 77.638 28.9736 77.6422
29.9828 78.1222 29.9756 78.1325
29.9789 78.0631 29.9608 78.0842
29.9789 78.0631 29.9608 78.0842
-重复输出,为什么会这样?