问题
In C, I want to create and open text files to write data into, but the problem is I want to name the files on the go, such as
FILE *ptr;
for(i=0;i<1000;i++){
fopen_s(&ptr,"i.txt","w");
operations to fill data into file i.txt;
fclose(ptr);
}
such that I will create file 0.txt, 1.txt, 2.txt ... 999.txt.
How is this possible? I checked open and rename functions, but couldn't find a way to do.
Thank you so much for all your help. Best,
回答1:
use snprintf
to set the file number:
FILE *ptr;
char name[FILENAME_MAX];
for(i=0;i<1000;i++){
snprintf(name, sizeof(name), "%d.txt", i);
fopen_s(&ptr, name, "w");
//operations to fill data into file i.txt;
fclose(ptr);
}
来源:https://stackoverflow.com/questions/13432535/how-to-name-and-create-file-in-a-loop