Dealing with a large number of files

So, I have to deal with around 200 files analyzing them and reformatting to a useable data output. My problem is, I don't know how to open this many in a for loop. right now I have
 
  vector <string> files [204];

as a global variable, but I know this is wrong, and to open all the files I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void openfiles()
{
	
     for (int i=0; i<204; i++) 
     {
    	file1=file[i];
        string name;
		name=names[i];
         if (i<=102)
         {
                    ifstream file1(name);
                
         }
         else
         {
             ofstream *file1(name);
        
         }
         file1.open(name);
     }
}

I realize that this is faulty, but what is the p[roper way to approach this?
Thank you
Last edited on
Do you have to have all 200 files open at the same time, or can you open one pair of files, process them, then close them and open the next pair?

If you have to have all 200 open at the same time and since 100 are input and 100 are output, you need two vectors:
1
2
  vector<istream>   i_files;
  vector <ostream> o_files; 


If you can process one pair at a time, then your vector<string> is fine, however, I would use two vectors:
1
2
  vector<string>  i_filenames;
  vector<string>  o_filenames;


Is it really necessary to open all the files at once?

Or can you open the files as required?

Opening a large number of files is very inefficient and you will probably be limited as to the maximum number of open files by your operating system.

It is unfortunately necessary to open all the files at once due to the analysis that needs to be run upon them (data from multiple runs separated into data types then put into ROOT).
Thank you AbstratcionAnon, I did not know that you could have a vector of istreams.
so final result...

in order for the files to open and close correctly, I replaced all file1 with just file[i] and the vector used was
 
vector <fstream> file;


Thank you for the help, the fact that you could do a fstream vector was the piece I needed.


EDIT: Upon Running, the files do not open. Just out of curiosity, does Dev-C++ have anywhere in particular in needs input files to be stored? (UPDATE: Never mind, just a problem with the names string vector)
Last edited on
Topic archived. No new replies allowed.