Hi,
Im guessing this is an easy piece of code, but i cant find the answer Im looking for anywhere.

I have 20 files each with a different a list of numbers (not all the same amount of numbers). Each file is called data-01.txt, data-02.txt... to data-20.txt.I need to open each file, do the same calculation on each file and print out all the results.

I was looking for a way to open each file by using a variable(i=1; i<=20; i++) rather than type in each, but can't seem to find one.

Any help please?

I would investigate the string library's function strcat() and functions like sprintf();

The key is in the file names themselves: data-01.txt, data-02.txt... to data-20.txt.

Use a char array fname[MAX] to construct each of your filenames:

char fname[12] = {"data-"}; //leave for for the end of string char: '\0'
char num[3] = {"00";
char ext[5][".txt"};
int fnumber;

then use a for loop, and strcat() to add on the next number: 
for fnumber = 1 to 20
  num = itoa(fnumber);
  printf num
  if(num[0] == 0) {
    num[0] = num[1];
    num[1] = '\0'; //end of string char
  end if
  strcat num onto the end of fname
  strcat ext onto the end of fname
  print fname to check it's doing it right
  open the file and process as normal here
end for

This approach works, but the above may not be as polished as I'd like. Post back if that pseudo code doesn't spark any flashes of light for you.

Im afraid ive never used the strcat function... I've had a look about and I vaugely understand its use but seemingly not enough to write a code that works. Thanks for the help... at least I know where to look :/

Just learn the use of strcpy, strcat and itoa function.. and it would be quite easy to implement such code..:)

Have a Look..

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int main()
{
 char fname[12];
 char num[3]={"00"};
 int i;
 for(i=1;i<=20;i++)
  {
   strcpy(fname,"data-");
   if(i<10)
   itoa(i,&num[1],10);
   else
   itoa(i,num,10);
   strcat(fname,num);
   strcat(fname,".txt");
   puts(fname);

 }

 return 1;
}

I fully understand the above code now... after much perserverance. Thankyou :)
Does this now mean i can use

input = fopen("fname", "r");

and it will open each .txt file in order?

I fully understand the above code now... after much perserverance. Thankyou :)
Does this now mean i can use

input = fopen("fname", "r");

and it will open each .txt file in order?

Yep, but without the quotes around fname as it is a variable name not a string itself.

input = fopen(fname, "r");

Ahhh works perfectly :)

Thankyou

Hey becka221,

Now your problem has been solved so mark this thread as solved...

main()
{
int k=35;
printf("\n %d %d %d",k==35,k=50,k>40);
}

for above code y d o/p is: 0 50 0 ?
i mean it must b 1,50,0 as d first comparison is true

main()
{
int k=35;
printf("\n %d %d %d",k==35,k=50,k>40);
}


for above code y d o/p is: 0 50 0 ?
i mean it must b 1,50,0 as d first comparison is true

please start new thread for this question.. don't ask your question in someone else thread.. :)

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.