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