Hey, guys. Can u help me with the piece of code? So, I must read a text file that contains the name of CSV files that will be in the same folder called "data". I've tried to do it, but it is just reading the first CSV file and not the other ones. I am storing the data within the CSV file in another class.
Here is the piece of code that I did:
int main()
{
std::ifstream file("data/met_index.txt"); //this is the TXT file containing the CSV files name
if(!file) return -1;
std::vector<std::string> csvFiles; //I decided to use a vector as I am not sure how many CSV files will be
std::string temp;
WindLog windlog;
for(int i = 0; i < csvFiles.size(); i++)
{
std::string fileName = "data/" + csvFiles[i]; //I am creating a string that will be "data/...CSVFILENAME" as I am reading only the CSV name in the TXT
std::ifstream inFile(fileName.c_str());
if(!inFile) return -1;
inFile >> windlog; //Here I am reading and storing the CSV file in another class
inFile.close();
}
for(int i = 0; i < csvFiles.size(); i++)
{
std::string fileName = "data/" + csvFiles[i];
std::cout << fileName << '\n'; // Put this line in and tell us what your program says.
The filestream doesn't go into a failed state until it has tried to read something ... by which time you are committed to running the loop. You will probably end up with the last entry repeated.
Yes, I have. I also tried to change the position in the TXT file of the CSV files names. For example, I have tried to put MeData_Jan01-2011-2012 first and it was reading fine, than I tried with 2012-2013 first and it was also reading fine. So, there is no problem in the CSV.
I have used this code:
I am reading just some columns of the CSV file. Moreover, I am saving each column in a different class. For example: first column I have date and time, and I am saving date in Date class and time in Time class.
#ifndef VECTOR_H
#define VECTOR_H
#include <vector>
template <class T>
class Vector
{
public:
Vector();
~Vector();
void add(T& obj); //addition function, it will add elements in the array
T getAt(unsigned index) const; //template function to return a element given a certain index
private:
int cap; //capacity integer, it will be resized and changed later
int nEl; //number of elements integer, it will check if the cap has been reached
std::vector<T> arr; //pointer template
};
template <class T>
Vector<T>::Vector()
{
this->cap = 500;
this->nEl = 0;
}
template <class T>
Vector<T>::~Vector()
{
}
template <class T>
void Vector<T>::add(T& obj)
{
this->arr.push_back(obj);
}
template <class T>
T Vector<T>::getAt(unsigned index) const
{
returnthis->arr[index];
}
#endif // VECTOR_H
This is a task for uni. My teacher wasn't allowing us to use STL Vector, so we needed to create our own template vector class. Now, I think I can use STL Vectors, but I think this code should read those files using this Vector Class.
Where?
At the moment you are reading each file successively into the same Windlog object.
WindLog creates a vector object of each class. For instance:
1 2 3 4 5 6 7 8
private:
Vector<Date> dates; //Date vector object
Vector<Time> times; //Time vector object
Vector<WindSpeed> speeds; //Wind speed vector object
Vector<SolarRadiation> rad; //Solar radiation vector object
Vector<AirTemperature> airT; //Air temperature vector object
int counter; //Counter integer to get number of elements
};
This is from my WindLog.h header file.
Therefore, I have a bunch of setters and getters and each setter is setting for a Vector.
Wouldn't it be easier to have a std::vector<Windlog>, where a Windlog is a struct or class containing scalar objects:
1 2 3 4 5 6 7 8
struct Windlog
{
Date date;
Time time;
double speed;
double rad;
double airT;
};
Also, there is nothing in the code you have shown that would allow you to know whether you have read all the files or not. Your Vector class is a bit unnecessary. You are just wrapping a std::vector.
I've just changed the objects trying to use STL Vector and it is still only reading the first CSV File. Alright, I will follow your advise and try to refactor it. Thanks so much for your help! Appreciate it!