Changing ifstream's source file in a while loop
Hi,
Say you have a program that opens multiple files and reads text from each of them. Is there any way to do this using a while loop, so that you use an integer for the number of each file, and then change what ifstream reads in by using this integer? Basically the following example should show what I'm trying to do.
1.txt is just 3 lines:
blah1
blah2
blah3
2.txt is also 3 lines:
whatever1
whatever2
whatever3
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
char stuff_extracted[99];
char filename[20] = "1.txt";
ifstream file_in(filename);
int filenumber = 1;
char string1[20];
while (1)
{
if (filenumber == 2)
{
strcpy_s(filename, "2.txt");
ifstream file_in(filename);
}
file_in.getline(stuff_extracted, 99);
cout << stuff_extracted << endl << endl;
if ((filenumber == 1) && (file_in.eof()))
filenumber = 2;
else if ((filenumber == 2) && (file_in.eof()))
return 0;
}
}
This may not seem necessary when you've only got 2 files but if you've got 10 files you want to read in, something like this would be more useful.
So can anyone help me out with this or have I not explained the problem well enough?
Thanks.