0

Hello All,

I have a different question from my previous post. The problem I have now is that I want to output multiple files, having the names output1, output2, output3,....etc. I have a loop in which I do some things that output to the output file, but what I can't do is relate the output file to the iteration counter, so that in each iteration it outputs the result in a file having the name of "outputn", where n is the iteration counter. Could you help me with that, please?

Thanks.
Aly

4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by jephthah
0

just use sprintf() to generate a new filename

char filename[255];
int i;
for(i = 0; i < 10; i++)
{
    FILE* fp;
    sprintf(filename,"file%d", i+1);
    fp = fopen(filename,"w");
    // blabla
}
0

Ancient Dragon has always got some great methods which come handy :) the above post by him is just superb. And ya if you want some predefined words as your file names for example in a business file like FINANCIAL, STRATEGIC and all you can feed it in the array defined as

char array[100][255];

and access it as

FILE *fp;
for(i=0;<n;i++)
{
fp=fopen(array[i],"w");
//your code
}
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.