So I've got a few txt files that I want to read in sequential order. The filename starts with "name_" and then a more specific tag, so "name_male","name_female", etc. Is there a way to load these files in sequential order? Psuedocode is as follows:
1 2 3
for each file with first classifier "name":
while (!file.eof()):
getline(some_buffer,file)
I know this can be done, since I've seen it in other programs. Plus, this will give me a great degree of flexibility later on down. Thanks in advance for the help!
The easiest way would be to get a list of filenames to read, then iterate through them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
vector<string> read_all_files( vector<string> filenames )
{
vector<string> result;
string line;
for (vector<string>::iterator filename = filenames.begin();
filename != filenames.end();
filename++
) {
ifstream file( *filename );
if (!file) continue;
while (getline( file, line )) result.push_back( line );
file.close();
}
return result;
}
If this is homework be aware that your professor will know that you didn't do this yourself if you just turn in this answer. So if it is homework, read it and understand it, then put it away and write your own version.