C++文件读写问题
代码如下:
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
ifstream fin( "1 ");
vector < pair <double, double> > t_s;
pair <double,double> tmp;
while(!fin.eof())
{
fin> > tmp.first> > tmp.second;
t_s.push_back(tmp);
}
fin.close();
ofstream fout( "t_s ");
vector < pair <double, double> > ::iterator iter;
for(iter=t_s.begin(); iter!=t_s.end(); ++iter)
fout < <setiosflags(ios::left) < <iter-> first < < '\t ' < <iter-> second < <endl;
fout.close();
}
测试用文件1的内容是:
1.111 2.222
2.222 3.333
输出文件的内容是:
1.111 2.222
2.222 3.333
2.222 3.333
多了一行数据,哪出了问题?
[解决办法]
while(fin> > tmp.first> > tmp.second)
{
t_s.push_back(tmp);
}