我正在尝试以下代码从文件中读取数据,然后尝试测试它们是否通过cout数据工作到屏幕,但我无法弄清楚这有什么问题。它只打印出我的数据文件的最后一行。你能看一下吗?感谢
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Data {
vector<double> cow_id;
vector<double> age_obs;
vector<double> dim_obs;
vector<double> my_obs;
vector<double> mcf_obs;
vector<double> mcp_obs;
vector<double> mcl_obs;
vector<double> bw_obs;
vector<double> bcs_obs;
void read_input (const string filepath)
{
ifstream data_in (filepath.c_str());
if (!data_in)
{cout<<"Failed to open"<<endl;}
else
{
double id,age, dim, my, mcf, mcp, mcl, bw, bcs;
string dummy_line;
getline(data_in, dummy_line);
while (data_in>>id>>age>>dim>>my>>mcf>>mcp>>mcl>>bw>>bcs);
{
cow_id.push_back(id);
age_obs.push_back(age);
dim_obs.push_back(dim);
my_obs.push_back(my);
mcf_obs.push_back(mcf);
mcp_obs.push_back(mcp);
mcl_obs.push_back(mcl);
bw_obs.push_back(bw);
bcs_obs.push_back(bcs);
}
data_in.close();
}
size_t size=age_obs.size();
for (size_t i=0; i<size; i++)
{
cout<<cow_id[i]<<'\t'<<age_obs[i]<<endl;
}
}
};
int main()
{
Data input;
input.read_input("C:\\Data\\C++\\learncpp\\data.txt");
}