Windows loop directory of files into array

This is a discussion on Windows loop directory of files into array within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    4

    Windows loop directory of files into array

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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Break it down into manageable steps.
    Code:
    void readAFile ( std::string filepath ) {
            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;
                }
                
            }
    }
    
    int main ( ) {
      readAFile( "somefile.txt" );
    }
    Get this to work for just one file.
    When it does work, then you use your directory reading code to call it repeatedly.

    As for your 3D array of strings, consider using std::vector for your storage, since a vector is a self-expanding data structure (where an array isn't).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4

    Cool

    Hey salem, thanks. I got the code working, by just hacking bits together 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?

    Code:
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <dirent.h>
    #include <sys/stat.h>
    
    using namespace std;
    
    int main ()
    {    
        string dir, filepath, line;
        DIR *dp;
        struct dirent *dirp;
        struct stat filestat;
    
        /* declare the array */
        vector < vector < vector < string > > > Data;
    
        /* open directory */
        dir = "c:\\data";
        dp = opendir( dir.c_str() );
        if (dp == NULL){
            cout << "Error opening directory" << endl;
            system("PAUSE");
            return errno;
        }
        /* loop files in directory*/
        int filenum = 0;
        while ((dirp = readdir( dp )))
        {
            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 the data file */
        	ifstream in( filepath.c_str() );
        	if(!in.is_open())
            {
                 cout << "Error opening file" << endl;
                 system("PAUSE");
                 return errno;
            } else {
                   
                Data.resize(filenum + 1);
         
                int row = 0;
        
            	while( getline(in,line) )
            	{
                    Data[filenum].resize(row + 1);
                    Data[filenum][row].resize(5);
        
                    string date, open, high, low, close;
                    istringstream liness( line );
                    
                    getline( liness, date,  ',' );
                    getline( liness, open,  ',' );
                    getline( liness, high,  ',' );
                    getline( liness, low,   ',' );
                    getline( liness, close, ',' );
                    
                    Data[filenum][row][0] = date;
                    Data[filenum][row][1] = open;
                    Data[filenum][row][2] = high;
                    Data[filenum][row][3] = low;
                    Data[filenum][row][4] = close;
                    
                    row = row + 1;
        
            	}
            	
            	filenum = filenum + 1;
            }
        }
        cout << Data[2][2000][0];
        
        system("PAUSE");
        return 0;
    
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    There is no need to use resize. Use push_back.
    And don't use the index operator; it's dangerous. Use the at member function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Windows directory with OPENFILENAME in Windows mobile
    By nikhilesh1987 in forum Windows Programming
    Replies: 2
    Last Post: 05-31-2011, 02:49 AM
  2. Get windows directory
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 07-05-2008, 05:19 AM
  3. printing directory in windows
    By hermit in forum Tech Board
    Replies: 6
    Last Post: 12-16-2004, 06:15 PM
  4. Array of Files in a Directory
    By JasonLikesJava in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2002, 11:28 AM
  5. directory and windows version?
    By davebaggott in forum Windows Programming
    Replies: 9
    Last Post: 10-13-2001, 02:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21