views:

117

answers:

5

Hi,

I have a little problem. I need to do some little operations on quite many files in one little program. So far I have decided to operate them in a single loop where I just change the number after the name. The files are all named TFxx.txt where xx is increasing number from 1 to 80. So how can I open them all in a single loop one after one? I have tried this:

for(i=1; i<=80; i++) {
   char name[8] = "TF"+i+".txt";
   FILE = open(name, r);
   /* Do something */
  }

As you can see the second line would be working in python but not in C. I have tried to do similiar running numbering with C to this program, but I haven't found out yet how to do that. The format doesn't need to be as it is on the second line, but I'd like to have some advice of how can I solve this problem. All I need to do is just be able to open many files and do same operations to them.

+4  A: 

You can use sprintf as follows:

for(i=0; i<=80; i++) {
   char name[32];
   memset(name, 0, sizeof(name));
   FILE *fp;
   sprintf(name, "TF%d.txt", i);
   fp = fopen(name, "r");
   /* Do something */
  }
Daniel Băluţă
Thanks, this worked. Solution seems to be a lot easier than I first thought. Maybe I was thinking this in a too complicated way :)
zaplec
The code is not quite correct since the name[] buffer will overflow by one (there is not enough space for the \0 terminator sprintf adds).It's recommended to use the snprintf() function, even though in this case sprintf will do fine (with a larger name buffer , of course).
Unknown
Of course I would go for snprintf, but for the beginning it's ok with sprintf.
Daniel Băluţă
moreover, just to add a comment, the use of #define or in general symbolic names instead of "hard encoded" numbers is always better...e.g. BUFLEN instead of 32, and MAXNUMOFFILES instead of 80...
ShinTakezou
kaizer.se
ShinTakezou: Why is that? I have sometimes wondered the meaning of the predefined numbers e.g. #define BUFLEN 32 as you said.
zaplec
@kaizer.se: because sprintf is not completely wrong. Also because when a beginner is introduced to a family of functions you should start with the simplest one. After some time, you let him discover why this function is not quite recommended and introduce more advanced one.
Daniel Băluţă
A: 

Maybe I am wrong, but just use fopen instead of open and write "r" instead of r?

FILE = fopen(name, "r");
TheMorph
You can also use open, but the second parameter should be something like O_RDONLY :). I will edit my answer.
Daniel Băluţă
Yes, but it's not ANSI-C then. ;)
TheMorph
+1  A: 

In addition to Daniels answer I would like to add that char name[8] should be little bigger to hold the terminating '\0' i.g. char name[20];

and FILE = open(name,r); should be FILE * fp = fopen(name,"r");

stacker
You are right :)
Daniel Băluţă
@daniel I see you changed that, fclose(fp) should also be called ;-)
stacker
+1  A: 

Let us suppose yours is just pseudocode; otherwise the problem is not only

char name[8] = "TF"+i+".txt";

where you use a sum to concatenate strings and convert an integer into string... (this is reasonable on some languages, absolutely not in C, where the + is just a sum between numbers)... but also FILE = open... is problematic...

char name[BUFLEN];
sprintf(name, "TF%d.txt", i);

would fill your name (snprintf(name, BUFLEN, "TF%d.txt") could be better, but it is C99, while the other is C89 too).

Files can be opened using something like FILE *fh = fopen(name, "r") for reading.

ShinTakezou
Yeah it's more or less pseudocode. That summing of strings works just like that on python, but as you said definetly not in C. I'm making a move from python to C and it doesn't seem to be very simple in all cases :)
zaplec
I think to learn C first, and then stick to some more "flexible" dynamic language like python, is easier :)... in C, forget objects of course, garbage collection, hashs and dynamic array, list comprehension, ... and more... everything can be done of course, and something easily using ext not standard libs... but it is a totally different experience.
ShinTakezou
A: 

And shouldn't char name[32] be outside the for loop?

Tangrs
That's right. All the definitions that needs to be done only once should be outside the loop.
zaplec