简体   繁体   English

从9个.csv文件读取和处理数据C ++

[英]Reading and Manipulate Data from 9 .csv files C++

I am currently having issue with trying to read and manipulate data from 9 .csv files, i was able to do it with 1 however i don't know how to do it with 9 data file. 我目前在尝试读取和操作9个.csv文件中的数据时遇到问题,我能够用1个做到这一点,但是我不知道如何使用9个数据文件来完成。 Below is the code on how i did it with 1 data file and i find it impractical to repeat it 9 times. 下面是关于我如何使用1个数据文件执行的代码,我发现将其重复9次是不切实际的。 Could anyone advise me? 谁能告诉我?

There are 9 different file names: 有9个不同的文件名:

  • Jan20071toDec31abcdefghijklmnopq.csv 从2007年1月1日到12月31日abcdefghijklmnopq.csv
  • Jan20081toDec31abcdefghijklmnopq.csv 从2008年1月1日到12月31日abcdefghijklmnopq.csv
  • Jan20091toDec31abcdefghijklmnopq.csv 从2009年1月1日到12月31日abcdefghijklmnopq.csv
  • MetData_Jan01-2010-Jan01-2011-ALL.csv MetData_Jan01-2010-Jan01-2011-ALL.csv
  • MetData_Jan01-2011-Jan01-2012-ALL.csv MetData_Jan01-2011-Jan01-2012-ALL.csv
  • MetData_Jan01-2012-Jan01-2013-ALL.csv MetData_Jan01-2012-Jan01-2013-ALL.csv
  • MetData_Jan01-2013-Jan01-2014-ALL.csv MetData_Jan01-2013-Jan01-2014-ALL.csv
  • MetData_Jan01-2014-Jan01-2015-ALL.csv MetData_Jan01-2014-Jan01-2015-ALL.csv
  • MetData_Jan01-2015-Jan01-2016-ALL.csv MetData_Jan01-2015-Jan01-2016-ALL.csv

      ifstream infile("Jan20071toDec31abcdefghijklmnopq.csv"); while (!infile.eof()) { infile.ignore(50, ' '); getline(infile, day, '/'); vecDay = atoi(day.c_str()); getline(infile, month, '/'); vecMonth = atoi(month.c_str()); getline(infile, year, ' '); vecYear = atoi(year.c_str()); getline(infile, hour, ':'); vecHour = atoi(hour.c_str()); getline(infile, minutes, ','); vecMinutes = atoi(minutes.c_str()); getline(infile, dp, ','); vecDP = atoi(dp.c_str()); getline(infile, dta, ','); vecDTA = atoi(dta.c_str()); getline(infile, dts, ','); vecDTS = atoi(dts.c_str()); getline(infile, ev, ','); vecEV = atoi(ev.c_str()); getline(infile, qfe, ','); vecQFE = atoi(qfe.c_str()); getline(infile, qff, ','); vecQFF = atoi(qff.c_str()); getline(infile, qnh, ','); vecQNH = atoi(qnh.c_str()); getline(infile, rf, ','); vecRF = atoi(rf.c_str()); getline(infile, rh, ','); vecRH = atoi(rh.c_str()); getline(infile, s, ','); vecS = atoi(s.c_str()); getline(infile, sr, ','); vecSR = atoi(sr.c_str()); getline(infile, st1, ','); vecST1 = atoi(st1.c_str()); getline(infile, st2, ','); vecST2 = atoi(st2.c_str()); getline(infile, st3, ','); vecST3 = atoi(st3.c_str()); getline(infile, st4, ','); vecST4 = atoi(st4.c_str()); getline(infile, sx, ','); vecSX = atoi(sx.c_str()); infile >> t; } 

EDIT (Add on) So far i have manage to store the file names in the vector but i can't seems to read it. 编辑(添加)到目前为止,我已经设法将文件名存储在矢量中,但是我似乎无法读取它。

string fileNames;
ifstream infile;

vector <string> vecFileNames
{
    "Jan20071toDec31abcdefghijklmnopq.csv",
    "Jan20081toDec31abcdefghijklmnopq.csv",
    "Jan20091toDec31abcdefghijklmnopq.csv",
    "MetData_Jan01-2010-Jan01-2011-ALL.csv",
    "MetData_Jan01-2011-Jan01-2012-ALL.csv",
    "MetData_Jan01-2012-Jan01-2013-ALL.csv",
    "MetData_Jan01-2013-Jan01-2014-ALL.csv",
    "MetData_Jan01-2014-Jan01-2015-ALL.csv",
    "MetData_Jan01-2015-Jan01-2016-ALL.csv"
};

for (unsigned i = 0; i < vecFileNames.size(); i++)
{
    fileNames = vecFileNames[i];

    cout << fileNames << endl;
    infile(fileNames); // <- Having problems
}

Add on (This is how the data was formatted) 加(这是数据格式化的方式)

UTC Dp Dta Dts EV QFE QFF QNH RF RH S SR ST1 ST2 ST3 ST4 Sx T UTC Dp Dta Dts EV QFE QFF QNH RF RH S SR ST1 ST2 ST3 ST4 Sx T

31/12/2006 1:00 9.3 50 16 934.6 1009 1012.4 1012.6 0 32.1 9 657 25.4 28.7 28.1 26 13 27.44 2006年12月31日1:00 9.3 50 16 934.6 1009 1012.4 1012.6 0 32.1 9 657 25.4 28.7 28.1 26 13 27.44

EDIT 编辑

Here is what i've tried so far, i created a date and time class which i have to used so i've edited the output and input a little. 这是到目前为止我尝试过的工作,我创建了必须使用的日期和时间类,因此我编辑了输出并输入了一些内容。 I've created a csv file with 1 record, however after i compile there was no error but no record was showing. 我创建了一个有1条记录的csv文件,但是在编译后没有错误,但没有记录显示。

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <iterator>
#include <utility>
#include <vector>
#include "binaryTreeType.h"
#include "bSearchTreeType.h"
#include "Data.h"
#include "Date.h"
#include "Time.h"

std::istream &operator>>(std::istream &is, char const *delim) {
if (is.flags() & std::ios::skipws) {
    while (isspace((unsigned char)is.peek()))
        is.ignore(1);
}

while (*delim && *delim == is.peek()) {
    ++delim;
    is.ignore(1);
}
if (*delim)
    is.setstate(std::ios::failbit);
return is;
}

struct record 
{
int day, month, year;
int hour, minutes;
double dp, ev, qfe, qff, qnh, rh, st[4], t;
int dta, dts, rf, s, sr, sx;

friend std::istream &operator>>(std::istream &is, record &r) {

    is >> r.day >> "/" >> r.month >> "/" >> r.year >> " " >> r.hour >> ":" 
>> r.minutes >> "," >> r.dp >> "," >> r.dta >> "," >> r.dts >> "," >> r.ev 
>> "," >> r.qfe >> "," >> r.qff >> "," >> r.qnh >> "," >> r.rf >> "," >> 
r.rh >> "," >> r.s >> "," >> r.sr;

    for (int i = 0; i < 4; i++)
    {
        is >> "," >> r.st[i];
    }
          is >> r.sx >> "," >> r.t;
    return is;
}

friend std::ostream &operator<<(std::ostream &os, record const &r) {

    os << r.day << "/" << r.month << "/" << r.year << " " << r.hour << ":" 
<< r.minutes << "," << r.dp << "," << r.dta << "," << r.dts << "," << r.ev 
<< "," << r.qfe << "," << r.qff << "," << r.qnh << "," << r.rf << ","      
<< r.rh << "," << r.s << "," << r.sr;

    for (int i = 0; i < 4; i++)
    {
        os << "," << r.st[i];
    }
          os << r.sx << "," << r.t;
    return os;
}
};

template <class Container>
void read_data(std::string const &name, Container &c) {
std::ifstream in(name);
record temp;
while (in >> temp)
{
    c.push_back(temp);
}
}

using namespace std;

int main()
{
   vector <string> filenames
  {
    "Test.csv"
    //"Jan20071toDec31abcdefghijklmnopq.csv",
    //"Jan20081toDec31abcdefghijklmnopq.csv",
    //"Jan20091toDec31abcdefghijklmnopq.csv",
    //"MetData_Jan01-2010-Jan01-2011-ALL.csv",
    //"MetData_Jan01-2011-Jan01-2012-ALL.csv",
    //"MetData_Jan01-2012-Jan01-2013-ALL.csv",
    //"MetData_Jan01-2013-Jan01-2014-ALL.csv",
    //"MetData_Mar01-2014-Mar01-2015-ALL.csv",
    //"MetData_Mar01-2015-Mar01-2016-ALL.csv"
    };

   vector <record> data;

for (auto && s : filenames)
{
    read_data(s, data);
}

for (auto const &r : data)
{
    cout << r << "\n";
}

Put the code in a function, and pass the name of the file to the function as a parameter: 将代码放入函数中,然后将文件名作为参数传递给函数:

void read_data(std::string const &filename) {
    ifstream infile(filename);

    // code to read dates
}

Then you can call that nine times: 然后,您可以调用九次:

std::vector<std::string> filenames { 
    "Jan20071toDec31abcdefghijklmnopq.csv",
    "Jan20081toDec31abcdefghijklmnopq.csv",
    "Jan20091toDec31abcdefghijklmnopq.csv",
    "MetData_Jan01-2010-Jan01-2011-ALL.csv",
    "MetData_Jan01-2011-Jan01-2012-ALL.csv",
    "MetData_Jan01-2012-Jan01-2013-ALL.csv",
    "MetData_Jan01-2013-Jan01-2014-ALL.csv",
    "MetData_Jan01-2014-Jan01-2015-ALL.csv",
    "MetData_Jan01-2015-Jan01-2016-ALL.csv"
};

for (auto && s : filenames)
    read_data(s);

Then, you probably want to look up std::get_time . 然后,您可能想要查找std::get_time It'll let you read the dates a lot more easily. 它使您可以更轻松地阅读日期。 Oh, and using while (!whatever.eof()) is almost always a bug (including the code here). 哦,使用while (!whatever.eof())几乎总是一个错误(包括此处的代码)。

Just for grins, I created a couple of test files with somewhat abbreviated forms of the data you seem to way, formatted like this: 只是出于笑容,我创建了一些测试文件,它们的格式似乎有些简化,格式如下:

4/4/2017 1:40, 54321, 5432, 543, 54, 5 
17/4/2017 12:47, 1, 12, 123, 1234, 12345 

I put one of those lines in each of two files, then ran the following code: 我将这些行之一放入两个文件中的每一个中,然后运行以下代码:

#include <vector>
#include <string>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <fstream>

std::istream &operator>>(std::istream &is, char const *delim) {
    if (is.flags() & std::ios::skipws) {
        while (isspace((unsigned char)is.peek()))
            is.ignore(1);
    }

    while (*delim && *delim == is.peek()) {
        ++delim;
        is.ignore(1);
    }
    if (*delim)
        is.setstate(std::ios::failbit);
    return is;
}

struct record {
    tm date;
    int dp, dta, dts, ev, qfe; // , qff, qnh, rf, rh, s, sr, st[4], sx, t;

    friend std::istream &operator>>(std::istream &is, record &r) {
        is >> std::get_time(&r.date, "%d / %m / %Y %H : %M");
        is >> "," >> r.dp >> "," >> r.dta >> "," >> r.dts >> "," >> r.ev >> "," >> r.qfe;
//      >> "," >> r.qff >> "," >> r.qnh >> "," >> r.rf << "," >> r.rh >> "," >> r.s >> "," >> r.sr;
//      for (int i = 0; i < 4; i++)
//          is >> "," >> r.st[i];
//      is >> r.sx >> "," >> r.t;
        return is;
    }

    friend std::ostream &operator<<(std::ostream &os, record const &r) {
        os << std::put_time(&r.date, "%Y/%m/%d %H:%M");
        os << "," << r.dp << "," << r.dta << "," << r.dts << "," << r.ev << "," << r.qfe;
        //      << "," << r.qff << "," << r.qnh << "," << r.rf << "," << r.rh << "," << r.s << "," << r.sr;
        //      for (int i = 0; i < 4; i++)
        //          os << "," << r.st[i];
        //      os << r.sx << "," << r.t;
        return os;
    }


};

template <class Container>
void read_data(std::string const &name, Container &c) {
    std::ifstream in(name);
    record temp;
    while (in >> temp)
        c.push_back(temp);
}

int main() {

    std::vector<std::string> filenames{
        "Jan20071toDec31abcdefghijklmnopq.csv",
//      "Jan20081toDec31abcdefghijklmnopq.csv",
//      "Jan20091toDec31abcdefghijklmnopq.csv",
//      "MetData_Jan01-2010-Jan01-2011-ALL.csv",
//      "MetData_Jan01-2011-Jan01-2012-ALL.csv",
//      "MetData_Jan01-2012-Jan01-2013-ALL.csv",
//      "MetData_Jan01-2013-Jan01-2014-ALL.csv",
//      "MetData_Jan01-2014-Jan01-2015-ALL.csv",
        "MetData_Jan01-2015-Jan01-2016-ALL.csv"
    };

    std::vector<record> data;

    for (auto && s : filenames)
        read_data(s, data);

    for (auto const &r : data)
        std::cout << r << "\n";
}

The result was this: 结果是这样的:

2017/04/17 12:47,1,12,123,1234,12345
2017/04/04 01:40,54321,5432,543,54,5

Note that I've re-formatted the output data a little (eg, changed it from day/month/year to year/month/day ). 请注意,我对输出数据进行了一些重新格式化(例如,将其从day/month/year更改为year/month/day )。 Making it work with more data fields should be mostly more repetitions of similar code (and likewise, making it work with more files should be mostly a matter of un-commenting the file names). 使它与更多数据字段一起使用应该主要是对相似代码的更多重复(同样,使其与更多文件一起使用应该主要是取消注释文件名的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM