C++11 读取文件并输出所选数据

C++11 读取文件并输出所选数据,c++11,codeblocks,ifstream,C++11,Codeblocks,Ifstream,我试图从CSV文件中读取数据并仅输出选定的数据(日期、时间和S(速度)。我的程序能够运行,但它以一种奇怪的方式循环。第1行、第1行和第2行、第1行、第2行和第3行等等。程序中显示的最终记录也不正确(0/3/2016 23:50).需要一些指导来纠正此问题 下面是记事本格式的示例数据。它包含数千行,但仅选取最后3行来说明我的问题。忽略下面的“行”,它只是告诉您哪一行是excel格式的。第1行是我忽略的标题,后面是数据 线路1:WAST、DP、Dta、Dts、EV、QFE、QFF、QNH、RF、RH

我试图从CSV文件中读取数据并仅输出选定的数据(日期、时间和S(速度)。我的程序能够运行,但它以一种奇怪的方式循环。第1行、第1行和第2行、第1行、第2行和第3行等等。程序中显示的最终记录也不正确(0/3/2016 23:50).需要一些指导来纠正此问题

下面是记事本格式的示例数据。它包含数千行,但仅选取最后3行来说明我的问题。忽略下面的“行”,它只是告诉您哪一行是excel格式的。第1行是我忽略的标题,后面是数据

线路1:WAST、DP、Dta、Dts、EV、QFE、QFF、QNH、RF、RH、S、SR、ST1、ST2、ST3、ST4、Sx、T

第2行:2016年3月31日23:30,11.4257,11,01012.91016.41016.5,0,63.5,5,13,26.4,25.8,25.6,26.1,7,18.48

第3行:2016年3月31日23:40,11.4250,15,01012.91016.41016.5,0,64.7,5,10,26.4,25.8,25.6,26.1,6,18.19

第4行:2016年3月31日23:50,11.4243,5,01012.71016.21016.3,0,65.8,3,11,26.3,25.8,25.6,26.1,5,17.95

电流输出: 2016年3月31日23:30 速度:5

2016年3月31日23:30 速度:5

2016年3月31日23:40 速度:5

2016年3月31日23:30 速度:5

2016年3月31日23:40 速度:5

2016年3月31日23:50 速度:3

2016年3月31日23:30 速度:5

2016年3月31日23:40 速度:5

2016年3月31日23:50 速度:3

2016年3月0日23:50 速度:3

最高速度:5

完成了

预期产量 2016年3月31日23:30 速度:5

2016年3月31日23:40 速度:5

2016年3月31日23:50 速度:3

最高速度:5

完成了

使用名称空间std;
类型定义结构
{
日期d;
时间t;
浮动速度;
}
风车型;
//声明速度最大值函数
ostream和操作员(istream和输入、WindLogType和w1);
int main()
{
字符串文件名;
ifstream输入;
filename=“Data.csv”;
open(filename.c_str());
输入。忽略(500,“\n”);
矢量风车;
字符串行、第2行、readDay、readMonth、readYear、readHour、readMinute;
浮球;
而(!input.eof())
{
getline(输入,readDay,“/”);
getline(输入,readMonth,“/”);
getline(输入,readYear,”);
getline(输入,readHour,':');
getline(输入,readMinute,,);
int day1=atoi(readDay.c_str());
int month1=atoi(readMonth.c_str());
int year1=atoi(readYear.c_str());
inthour1=atoi(readHour.c_str());
int minute1=atoi(readMinute.c_str());
浮球s1;
对于(int i=0;i>s1;
输入。忽略(50,,);
}
WindLogType T1;//创建记录
T1.d.设定日期(第1天、第1个月、第1年);
T1.t.设定时间(小时1,分钟1);
T1.速度=s1;
windlog.push_back(T1);//在向量内推
print();
getline(输入,第2行,'\n');
}
浮动最大速度;
风车型H1;
H1=风车。在(0)处;
maxSpeed=H1.0速度;
对于(int i=0;imaxSpeed)
{
maxSpeed=风车在(i)处的速度;
}
}

coutOn一个不相关的注释,请记住类和结构名称也是类型名称。因此执行
struct WindLogType{…};
就足以将
WindLogType
作为一个类型引入。不需要
typedef
。我还建议您阅读。以及调用
windlog.print()
在循环的每一次迭代中,对于每一行。每次,它都会打印迄今为止累积的整个列表。您可能希望在处理完所有数据后调用它一次。
using namespace std;

typedef struct
{
Date d;
Time t;
float speed;
}
WindLogType;

//declare speed max function
ostream & operator <<(ostream &osObject, const WindLogType & w1);
istream & operator >>(istream &input, WindLogType &w1);

int main()
{
string filename;
ifstream input;

filename = "Data.csv";

input.open(filename.c_str());
input.ignore(500,'\n');

Vector<WindLogType> windlog;

string line,line2, readDay, readMonth, readYear, readHour, readMinute;
float sp;


while(!input.eof())
{

    getline(input,readDay,'/');
    getline(input,readMonth,'/');
    getline(input,readYear,' ');

    getline(input,readHour,':');
    getline(input,readMinute,',');


int day1 =atoi(readDay.c_str());
int month1=atoi(readMonth.c_str());
int year1=atoi(readYear.c_str());
int hour1=atoi(readHour.c_str());
int minute1=atoi(readMinute.c_str());

float s1;

for(int i = 0;i<10;i++)
{
    input>>s1;
    input.ignore(50,',');
}

WindLogType T1;//create a record
T1.d.setDate(day1,month1,year1);
T1.t.setTime(hour1,minute1);
T1.speed = s1;

windlog.push_back(T1);//push inside vector
windlog.print();

getline(input,line2,'\n');
}
float maxSpeed;
WindLogType H1;
H1=windlog.at(0);
maxSpeed=H1.speed;

for(int i=0;i<windlog.size();i++)
{
    if(windlog.at(i).speed>maxSpeed)
    {
        maxSpeed=windlog.at(i).speed;
    }
}
cout<<"Max Speed: "<<maxSpeed<<endl;

cout<<"Done!"<<endl;


return 0;
}

ostream & operator <<(ostream &osObject, const WindLogType &w1)
{
osObject<<w1.d<<w1.t<<endl<<"Speed: "<<w1.speed;
return osObject;
}

istream &operator >>(istream & input, WindLogType &w1)
{
input>>w1.d>>w1.t>>w1.speed;
return input;
}