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. Below is the code on how i did it with 1 data file and i find it impractical to repeat it 9 times. Could anyone advise me?
There are 9 different file names:
- 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; }
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
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
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.
#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";
}