How to create multiple .txt files?

1

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

    
asked by anonymous 30.09.2016 / 06:05

1 answer

4

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);
        }
    }
}
    
30.09.2016 / 10:28