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!!



thanks Kate :confused:

Recommended Answers

All 4 Replies

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;

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.

ah ha! nice one!

you're a lifesaver thankyou.


Dave beat me by one minute! Rats! :-)

Ah, well, great minds.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.