How to sequentially read all the files in a directory

This is a discussion on How to sequentially read all the files in a directory within the C++ Programming forums, part of the General Programming Boards category; Hi, I've done a lot of searching and haven't found any good ideas about this. It must be simple. I'm ...

  1. #1
    Registered User Olaveson's Avatar
    Join Date
    Apr 2010
    Posts
    2

    How to sequentially read all the files in a directory

    Hi,
    I've done a lot of searching and haven't found any good ideas about this. It must be simple. I'm not an experienced C++ programmer and I haven't quite grasped the concepts of data abstraction and classes, so I'm probably still thinking with the old "top-down" paradigm.

    Admittedly, by getting some code from the "snippets" page and other internet sources, I'm trying to work with functions that I don't thoroughly understand.


    Here's a description of what I want to do.

    Loop (in a directory)
    read file names in the directory into file_name[i] array
    end of loop

    Loop through input file_name[] array
    open each input file
    read about 250 lines of data into arrays
    zero out math variables
    do some math
    perform some tests
    if pass all tests?
    output file_name[i] to a an output file
    close file

    end of loop

    The following came from the snippets page and is used to read
    all the filenames in the directory. It works.
    I added the lines to load the file_name[] array which also works.
    I could probably skip that step and do all my operations within the
    while(FindNextFile....)loop, but keeping the steps separate is easier
    for me to keep organized.

    Code:
    // read all the filenames in a directory
    //************************************************************
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    char DirSpec[MAX_PATH]; // directory specification
    
    cout<<"Path: ";
    cin.get(DirSpec, MAX_PATH);
    cout<<"\n";
    strncat(DirSpec, "\\*", 3);
    hFind = FindFirstFile(DirSpec, &FindFileData);
    
    	if(hFind == INVALID_HANDLE_VALUE)
    	{
    	cout<<"Error: invalid path\n";
    	}
    
    
    //
    // Read the files in the directory and assign
    // the filenames to the file_name[] array
    
    int i;
    string file_name[7000];  
    //yes, I need to learn how to handle a variable length string array!
    //number of files could vary but will be around 6500
    
        i = 0;
        int file_count = 0;
    
    	while(FindNextFile(hFind, &FindFileData) != 0)
    	{
        file_name[i] = FindFileData.cFileName;
    	//cout<<FindFileData.cFileName<<"\n";
    	i++;
    	file_count++;
    	}
    
    
    FindClose(hFind);
    
    //output one example
    cout<<"file 12 is "<<file_name [12]<<"\n\n";  //this works
    
    //***********************************************************
    After I fill the file_names[] array, I want to loop through the files, read
    lines of data and perform operations. This is the part I haven't figured out.


    I'm trying to use the following as the basis to loop through the files
    My test files are consistent with the averaging routine here
    but this is just a test:

    Code:
    //************************************************************
    #include <iostream>
    #include <fstream>   // needed for files
    using namespace std;
    
    
    int main(void)
       {
       fstream InFile;
       float Num, Total;
       int Count;
    
    // I want to put the following into a loop and somehow replace "readfile.txt"
    // with file_name[j]
    
       InFile.open("readfile.txt", ios::in);
       if (InFile.fail())
          {
          cout << "Could not open readfile.txt" << endl;
          exit(1);
          }
    
       Count = 0;
       Total = 0.0;
    
       InFile >> Num;
    
       while (! InFile.fail())
          {
          Total = Total + Num;
          Count++;
          InFile >> Num;
          }
    
       if (Count > 0)
          cout << "Average is " << Total / Count << endl;
       else
          cout << "No data given" << endl;
    
       InFile.close();
       return 0;
       }
    //***********************************************************
    I guess my real question is: Is the Infile.open("readfile.txt"....) line
    extendable so that it could be used to loop through the file_name[] array?
    Sort of like the following (this doesn't work, it's just to get the idea
    of what I'm trying to do):

    Code:
    For ( j = 1; j <= file_count; j++)
    	{
    	InFile.open(file_name[j], ios::in);
    	//read lines of data
    	//perform operations
    	InFile.close();
            }
    Alternately, does anybody know a good C++ book where you can look up what you are trying to do and find some good examples of how to do it.

    Thanks,
    Rich
    Last edited by Olaveson; 04-24-2010 at 02:49 PM. Reason: trying to add code tags

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    .open requires a char * (a/k/a a C-style string), not a std::string, for the first parameter. So you'll have to convert:
    Code:
    InFile.open(file_name[j].c_str(), ios::in);

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        const char* pString1 = "test.txt";
        const char* pString2 = "test2.txt";
    
        const char* file_name[] = {pString1, pString2};
        int file_count = sizeof(file_name) / sizeof(char*);
    
        for (int i = 0 ; i < file_count; i++)
        {
            float fValue = 0.0f;
            ifstream inFile(file_name[i]);
            while (!inFile.eof())
            {
                inFile >> fValue;
                cout << fValue << endl;
            }
            cout << endl << endl;
        }
    
        return 0;
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Be sure to read this, as well, before making any more replies: << !! Posting Code? Read this First !! >>
    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.

  5. #5
    Registered User Olaveson's Avatar
    Join Date
    Apr 2010
    Posts
    2
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Thank you Elysia" << endl;
        cout << "This is much nicer!" << endl;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help understanding how to read files.
    By TaiL in forum C Programming
    Replies: 1
    Last Post: 10-05-2008, 06:59 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Finding out how many files are in a directory
    By mikie7boy in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 05:08 AM
  4. open directory and copy files
    By 5n4k3 in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 09:49 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 01:51 PM

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