0

I have a project completed where I read in a file and assigns the data in the txt file to a matrix. The problem I am having is that I have 8 data files named data1.txt through data8.txt. I need to find a way to loop through the the data file names by somehow incrementing the number portion of the filename. I've tried to read multiple data file using strcpy, there does not seem anything error with my program, but it can't read the file. How I can fix it? Am I wrong when using 'strcpy'?

char filename[10];
for(input = 1; input <= 5; input++){
strcpy(filename,"data."+input);
ifstream fin(filename);
}

or may be any other to read multiple data.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

If you are trying to append the string corresponding to the integer value of input to the filename, you are not going to get it using strcpy.

There are a few ways you can do it, but one of the easiest:

stringstream ss;
ss << filename << "data." << input;
ifstream fin(ss.str());
Zac Howland
  • 15,777
  • 1
  • 26
  • 42