if they are all the same format, as you said, just make a struct of that format.
struct data
{
string first, second;
int third, fourth;
};
vector<data> filedata(1000); //whatever initial size makes sense for your stuff.
//you can typedef the above and treat it like an object if that appeals to you.
…
loop over file
file >> filedata[index].first >> filedata[index].second … etc //you can make your own >> operator to do this, but is it worth the trouble here?
if you don't know a max size (1000 above) you may need to use push back (ugg) and let it grow. Sometimes you can't avoid that.
#include <stdio.h>
#include <dirent.h>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
usingnamespace std;
int main(void)
{
struct datas
{
string firstname;
string lastname;
int weight;
int age;
};
vector<datas> filedata(1000);
int counter = 0;
// Pointer for directory entry
struct dirent *de;
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");
// opendir returns NULL if couldn't open directory
if (dr == NULL)
{
cout << "Could not open current directory" << endl;
cin.get();
return 0;
}
while ((de = readdir(dr)) != NULL)
{
string name(de->d_name);
// Search directory for all files ending in .csv
if (name.size() > 4 && name.substr(name.size() - 4) == ".csv")
{
cout << name << ":\n";
ifstream fin(name); //bring in file
string line;;
while (getline(fin, line)) { // To get the number of lines in the file
counter++;
}
datas* data = new datas[counter]; // Add number to structured array
for (int i = 0; i < counter; i++)
{
fin >> data[i].firstname >> data[i].lastname >> data[i].weight >> data[i].age;
cout << data[1].firstname << data[1].lastname << endl;
}
cout << data[1].firstname << data[1].lastname << endl;
}
}
cin.get();
closedir(dr);
return 0;
}