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

Recommended Answers

All 3 Replies

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
}

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
}

^ egads. you just walled off 25K of memory for ... what?

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.