I have a function:
void ReadInput(const char * name)
{
ifstream file(name);
cout << "read file " << name << endl;
size_t A0, A1, A2;
file >> A0 >> A1 >> A2;
}
Now i want to read two files: INPUT1.txt and INPUT2.txt in a loop, such as:
int main ()
{
for (int i = 1; i < 3; i++){
ReadInput(INPUT$i);
}
return 0;
}
The question is how do i define the loop correctly.
Thanks for your time in advance.
Here is the whole code:
#include <iostream>
#include <string>
using namespace std;
void ReadInput(const string& _name){
ifstream file(_name);
size_t A0, A1, A2;
file >> A0 >> A1 >> A2;
}
int main ()
{
for (int i = 1; i < 3; ++i) {
string file_name = "INPUT" + to_string(i) + ".txt";
ReadInput(file_name);
}
return 0;
}
OK, all good, now i can compile in c++98 by converting string to const char and stringstream instead of to_string. My goal was to run an automated program with input files all in the same directory. The suggestions about possible duplicate of the question does not achieve that as i have to pass on the input file number as i execute, as i understand it, which is impractical for 3,000 something files.