Reading from multiple txt files (i.e 100 txt files) with one ifstream

Hello,

Any ideas of how to read from multiple txt files (i.e 100 txt files) with one ifstream? Is there a loop for that?

ifstream fin("filename.txt");
if (!fin) {
return (cout << "cannot open file\n"), 1;
}
.
.
.

Thank you!
If your files are located in a single directory, or a directory with subdirectories, iterating for files with C++17's <filesystem> library would make it easier to deal with multiple files.
Maybe sth. like that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    std::ifstream src;
    
    for (int i = 0; i < 100; ++i)
    {
        std::string fname = "File" + std::to_string(i);
        src.open(fname);
        if (src)
        {
            // use src;
            src.close();
        }
        else
        {
            //handle error
        }
    }
}
@furry guy: Thanks, I will take a look at it

@thmm: Thanks thmm, the thing is that I have already tried this and it doesn't work. For some reason it sticks with the

if (!src) { cout << error open file" <<endl;}

, plus, I have a long list of files (400) and therefore, since I have to store each data I read to a vector, I will have to break it into three vectors (0-9, 10-99, 100-400) and then concatenate the three vectors.
Last edited on
Along that same line. It is easy to extend it to an array of fstream's and use functions to eliminate repetition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>

int main()
{
    const int NO_FILES{100};
    std::fstream file;
    
    // SETUP FILES
    for(int i = 0; i < NO_FILES; i++)
    {
        file.open ("test" + std::to_string(i) + ".txt" + std::to_string(i) + ".txt", std::ios::in | std::ios::out|std::ios::app);
        file << i << '\n';
        file.close();
    }
    
    // READ FROM A FILE
    int number{0};
    
    int j{2};
    file.open("test" + std::to_string(j) + ".txt");
    file >> number;
    std::cout << number << '\n';
    file.close();
    
    j = 1;
    file.open("test" + std::to_string(j) + ".txt");
    file >> number;
    std::cout << number << '\n';
    
    return 0;
}
I will have to break it into three vectors (0-9, 10-99, 100-400) and then concatenate the three vectors.


Why? What are the names of the files you want to open/process?
@seeplus: Thanks a lot seeplus. I created another post with my code so far, with the name <<Plz your help!>>. I have the code there with the explanation of what I want to do, if you would like to advice me on that. Thanks in advance

@againtry: Thanks againtry. basically the files are already present, I don't have to set them up, and their name is dataK, where K starts from 000 and ends up to 150. In addition, I didn't quite understand the following:

file.open ("test" + std::to_string(i) + ".txt" + std::to_string(i) + ".txt", std::ios::in | std::ios::out|std::ios::app);

This isn't going to give something like for example test0.txt0.txt ?

Last edited on
@geovoulg
The line should be:
file.open ("test" + std::to_string(i) + ".txt", std::ios::in | std::ios::out|std::ios::app);

As far as the meaning of that line is concerned, see http://www.cplusplus.com/doc/tutorial/files/

Last edited on
You probably shouldn’t be reading all those files with a single stream object. If you find yourself calling open/close on fstream’s, you’re likely doing it wrong.
Wouldn't it be better performance-wise than creating and destroying hundreds of ifstream objects?
What would you suggest?
You wouldn’t create hundreds of stream objects at the same time, you’d create the one at a time on demand.

Let the constructor initialize the object and the destructor cleanup.

This may give you insight to treat the streams more generally, and use the Creator pattern to instantiate the right kind of stream. Dunno what your exact requirements are, but it’s often worth thinking more widely about such things.

EDIT: Thanks to IOS autocorrect for making my spelling even worse.
Last edited on
Thank you all for your replies here. You really helped me with your tips!
@geovoulg

Perhaps it might be better if you don't create a new topic each time for related questions, just put it all in one topic rather than 4 of them :+)

I hope you like the code I posted in the other topic, and I hope it all goes well for you and your project - Cheers :+)
@TheIdeasMan

Thank you for the code and I apologize for creating to many topics, I just thought that since are different (even though they were on the general same topic of pairs) it would be good not to mess them up.

Could you please give me an idea on the following as seeplus did help me but the cout was not obtaining the pairs as pairs?

Suppose we have a vector of pairs like the following:

V { (1,10) (4,20) (3,50) (6,40) (8,50) (10,30) (7,40) (12,50) };

after sorting the vector with the sort algorithm based on the second of pairs we have:

V { (1,10) (4,20) (10,30) (6,40) (7,40) (3,50) (8,50) (12,50) };

My question is, if we would like to find the first (or last) two consecutive pairs in the vector of pairs with greatest equal second, namely here pairs (3,50) and (8,50), and obtain them and store them in different pair variables sperately, i.e

pair A {(3,50)
pair B {8,50)}

,any ideas on how we handle this?

Thanks in advance
Last edited on
I saw it. Thanks really a lot for helping me out!
Topic archived. No new replies allowed.