hi
I have many files in a directory, just normal csv files like this:
aaaaaaaa,1111,1111,1111,1111,1111,1111
aaaaaaaa,1111,1111,1111,1111,1111,1111
etc
I need to read all files into a jagged array. All files are the same, but with different number of rows... I tried writing the code, but tbh I havent a clue what I am doing, so the code doesnt work. This is my first time writing c++. I tried looking for examples online but they are too complex to understand... If someone knows a good way to achieve what I want then it would be really helpful. thanks Megan
Code:#include <cstdlib> #include <iostream> #include <sstream> #include <fstream> #include <vector> #include <string> #include <dirent.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> using namespace std; // loop directory of files // read each file contents to array // store data in a jagged array int main () { string dir, filepath; int num; DIR *dp; struct dirent *dirp; struct stat filestat; string Data[40][1000][4]; /* open directory */ dp = opendir( "c:/data" ); if (dp == NULL){ cout << "Error(" << errno << ") opening " << dir << endl; system("PAUSE"); return errno; } /* loop files in directory*/ int filenum = 0; while ((dirp = readdir( dp ))) { filenum++; filepath = dir + "/" + dirp->d_name; // If the file is a directory (or is in some way invalid) we'll skip it if (stat( filepath.c_str(), &filestat )) continue; if (S_ISDIR( filestat.st_mode )) continue; /* open file */ ifstream inFile ( filepath.c_str() ); string line; int linenum = 0; while (getline (inFile, line)) { linenum++; istringstream linestream(line); string item; int itemnum = 0; while (getline (linestream, item, ',')) { itemnum++; Data[filenum][linenum][itemnum] = item; } } } closedir( dp ); cout << Data[0][0][0]; system("PAUSE"); return 0; }



LinkBack URL
About LinkBacks



I did what you recommended, concentrated on one file and read about using vectors. It seems to be working the way I want. Can anyone help make it cleaner, and more efficent?