0

hi there. I've been battling with this for a while now, and really need some help!!

I have five files full of 100 numbers each, they are labelled 1.txt, 2.txt up to 5.txt.

I want to read these files in and put them all in an array: files[5][100]
so I can do some stats on the numbers.

aaarrgh! I cant find a neat way to read in, I want it to go like this I think, but obviously the command "infile.open("i.txt")" is bollox and doesn't work so please please please help!!

for(i=0;i<5;i++){
infile.open("i.txt");


counter=0;
while(!infile.eof){
infile>>x;
file[counter]=x;
counter++;
}
infile.close;
}

thanks Kate :confused:

Edited by happygeek: fixed formatting

3
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by Chainsaw
0

Try something along this line:

#include <fstream>
#include <cstdio>
using namespace std;

int main()
{
   char filename [ FILENAME_MAX ];
   for ( int i = 1; i <= 5; ++i )
   {
      sprintf(filename, "%d.txt", i);
      ifstream file(filename);
      // ...
   }
   return 0;
}
0

how about:

char filename[ 256 ]; // or MAX_PATH or whatever
...
sprintf( filename, "%d.txt", i ); // now filename will be 1.txt, 2.txt, whatever.

and run the loop from 1 to < 6 or 1 to <= 5, or else add 1 to i in the sprintf so you don't end up with 0.txt through 4.txt.

0

ah ha! nice one!

you're a lifesaver thankyou.

kate

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.