Looping through files

Hello,

I'm trying to run several files through a simulation, where each file name has letters and numbers in them. Suppose I have four files, one1.dat, one2.dat, two1.dat, two2.dat. I'm having trouble looping through the letter part of the file names. I'm looking for something like the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int fileindex=1;
loop through the letter parts of the file names (say the current one is filename) {
  while(fileindex<3){
    //I have been using the following code to open files, where I would write in
    //"one" for filename for example
    char outputArray[200];
    string outputFilename = filename;
    strcpy_s(outputArray,outputFilename.c_str());
    stringstream num;
    num<<fileindex;
    strcat_s(outputArray,num.str().c_str());
    strcat_s(outputArray,".dat");

    ifstream data (outputArray);
    //stuff...
    data.close(); 
    //more stuff

    fileindex++;
  }
}


Thank you!
Err... Why not simply
1
2
3
stringstream num;
num <<filename<<fileindex<<".dat";
ifstream data (outputArray.str().c_str());
?
use stringstream to form the entire name:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <sstream>

using namespace std;

int main(int argc, char **argv)
{
    for (int i=1; i<5; ++i) {
	stringstream ss;
	ss << "one" << i << ".dat";
	string filename = ss.str();
	cout << filename << '\n';
    }
    return 0;
}


Output:
one1.dat
one2.dat
one3.dat
one4.dat

Thank you both for your reply!
Helios, I guess my problem is I don't know how to store the letter parts of the file name so that I could loop through it. So filename should come from an array or a vector, but I haven't been able to figure out which one and what type.

Dhayden, I can loop through the number parts, but not the letter parts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
#include <string>
#include <iostream>
#include <sstream>
using namespace std;

const int MAXNUM = 3;
const string numbers[MAXNUM] = 
{  "one",
    "two",
    "three"
};

int main ()
{  string filename;
   for (int i=0; i<MAXNUM; i++)
  { stringstream ss;  
     ss << numbers[i] << i+1 << ".txt";
    filename = ss.str();
    cout << filename << endl;
  }
    system ("pause");
}
one1.txt
two2.txt
three3.txt
Press any key to continue . . .
Thank you very much, this is exactly what I needed!
Topic archived. No new replies allowed.