optomus
optomus

Reputation: 66

File Names Stored in an array and need to iterate through array creating a new file based on the name stored in each cell in C

I have 80+ text files which I am current'y reading into the program using dirent.h. This is placing them in an array, I am throwing them each through an algorithm which will compare the contents of each file to the other contents within that file and provide me with a percentage of how identical things within the file are. While doing that it will store the name of the file in ArrayName[i] and the percentage in ArrayPercent[i].

I am pretty sure I know how to print those array values to a file, that is not the problem, the problem is using a variable as the filename. My current thoughts are something along the lines of

fprintf(**DIRECTORY HERE**,"%s %d", ArrayPercent[i], ArrayName[i][]);

The first time through a loop ArrayName[0] needs to go in DIRECTORY HERE as the filename.txt and the second time through ArrayName[1] needs to go there as filename1.txt for argument sake and so forth for the remainder of ArrayName.

Upvotes: 0

Views: 111

Answers (1)

manav m-n
manav m-n

Reputation: 11394

You can create multiple filename0.txt, filename1.txt, etc using:

for (i=0; i<n; i++) {
    char buf[32];
    sprintf(buf, "filename%d_%s_%d", i, ArrayName[i], ArrayPercentage[i]);
}

Upvotes: 1

Related Questions