I am quite new to programming, I need some advice on how to automatically name a file using C. Since I need to run the same source code for several times on a cluster computer. To prevent overwriting on the same output file, I have to make each output file has a different name.
void RecordNumber()
{
srand(time(NULL));
FILE* fopen(), * fp;
char name[64];
int x, y;
long num[256];
int na = rand();
sprintf(name, "num[%d].dat", na);
if (first_num) {
first_num = 0;
fp = fopen(name, "w");
}
else
fp = fopen(name, "a");
for (x = 0; x <= species; ++x)
num[x] = 0;
for (x = 1; x <= xfield; ++x)
for (y = 1; y <= yfield; ++y)
++num[state[x][y]];
fprintf(fp, "\n%d", thetime);
for (x = 0; x < species; ++x)
fprintf(fp, "\t%d", num[x]);
fclose(fp);
}
I write an int "na" to store a random number, but the random number does not show in the file name (the file name is still "dat").
void RecordNumber() is stored in the library. In one of my code, I use void Report(){} to call the RecordNumber(0) function to record all the numbers in the simulations
void Report()
{
for (int j=j=thetime;j<=10000;j=j+1)
{
if (j%100==0)
{EchoTime(0);
CountSpecies(0);
RecordNumber(0);
}
else
break;
}
}
I want the file name looks like "dat_%d", what should I do?