I have a bunch of text files in a directory, and each text file is named "info1.txt", "info2.txt" and so on. How would I open up all of the text files in an array of ifstream objects without having to hard-code all my text file names in? I know the following code does not work, but I think it conveys the idea of what I want to do if it did work:
ifstream myFiles[5];
for(int i = 0; i < 5; i++){
myFiles[i].open("info" + i + ".txt");
}
I know the solution is probably very simple, but after a lot of research, trial and error I still haven't figured it out. Thanks!
For building the file names, I'd use std::ostringstream
and operator<<
.
If you want to use a container class like std::vector
(e.g. because you don't know at compile time how much big the array of ifstream
's will be), since std::ifstream
is not copyable, you can't use vector<ifstream>
, but you can use vector<shared_ptr<ifstream>>
or vector<unique_ptr<ifstream>>
; e.g.:
vector<shared_ptr<ifstream>> myFiles;
for (int i = 0; i < count; i++)
{
ostringstream filename;
filename << "info" << i << ".txt";
myFiles.push_back( make_shared<ifstream>( filename.str() ) );
}
With unique_ptr
(and C++11 move semantics):
vector<unique_ptr<ifstream>> myFiles;
for (int i = 0; i < count; i++)
{
ostringstream filename;
filename << "info" << i << ".txt";
unique_ptr<ifstream> file( new ifstream(filename.str()) );
myFiles.push_back( move(file) );
}
unqiue_ptr
is more efficient than shared_ptr
, since unique_ptr
is just a movable pointer, it's not reference counted (so there is less overhead than shared_ptr
). So, in C++11, you may want to prefer unique_ptr
if the ifstream
's are not shared outside the vector
container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With