How do I create multiple .txt files in c / c ++? I have this function:
tocopy(){
FILE *file = fopen("Teste.txt", "w");
fclose(file);
}
But it only creates a test.txt file
How do I create multiple .txt files in c / c ++? I have this function:
tocopy(){
FILE *file = fopen("Teste.txt", "w");
fclose(file);
}
But it only creates a test.txt file
In C, you can, for example, loop in which you generate different file names. If you always create a file with the same name, the Operating System will only save the most recent file.
#include <stdio.h>
#include <stdlib.h>
void tocopy(void) {
int k;
for (k = 0; k < 100; k++) {
char filename[100];
FILE *file;
sprintf(filename, "Teste-%02d.txt", k); /* Teste-00.txt; Teste-01.txt; ...; Teste-99.txt */
file = fopen(filename, "w");
if (file != NULL) {
fclose(file);
} else {
perror(filename);
exit(EXIT_FAILURE);
}
}
}