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

时间:2017-07-11 03:35:47

标签: c++

我目前遇到了尝试从9个.csv文件中读取和操作数据的问题,我能够用1做,但我不知道如何用9个数据文件做。下面是关于我如何使用1个数据文件的代码,我发现重复9次是不切实际的。有人可以告诉我吗?

有9种不同的文件名:

  • 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

        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;
          }
    

编辑(加入) 到目前为止,我已经设法将文件名存储在向量中,但我似乎无法阅读它。

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
}

添加(这是数据的格式化方式)

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

修改

这是我到目前为止所尝试的,我创建了一个日期和时间类,我必须使用,所以我编辑了输出并输入了一点。我创建了一个包含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";
}

1 个答案:

答案 0 :(得分:0)

将代码放入函数中,并将文件名作为参数传递给函数:

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

    // code to read dates
}

然后你可以打九次电话:

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);

然后,您可能想要查找std::get_time。它可以让你更轻松地阅读日期。哦,使用while (!whatever.eof())几乎总是一个bug(包括这里的代码)。

只是为了笑容,我创建了几个测试文件,其中包含您似乎有点缩写形式的数据,格式如下:

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

我将其中一行放在两个文件的每一个中,然后运行以下代码:

#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";
}

结果如下:

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

请注意,我稍微重新格式化了输出数据(例如,将其从day/month/year更改为year/month/day)。使其适用于更多数据字段应该更多地重复相似的代码(同样,使其适用于更多文件应该主要是取消注释文件名)。