want to read more than 1 .txt file in C [closed]

1

I wanted to create multiple .txt files and did this through a loop, and I did. But the only way I can read all the files is by doing another loop, for example: it does a looping of 3 passwords, that is, I created 3 passwords, 3 .txt files, but at the time of logging in I do not want to have to put the 3 passwords, I want my program to read the files and verify that the only password I put in the login hits one of the passwords already saved by the loop.

    
asked by anonymous 03.11.2018 / 03:35

2 answers

1

In C, the type used to communicate through files is FILE * as you should know, FILE * is a pointer to a struct that contains data from a certain file, given these traded in> between your program and the operating system; in other words, FILE * points to a file in the same way that void * points to a memory region. Multiple pointers can point to a memory region, but a pointer can not point to multiple memory regions (pointer vector - or second-order pointer - in this case).

The solution to your question would be to manipulate one file at a time, as you do this can be in many ways, but the smartest way would be a vector file.

    
03.11.2018 / 03:57
0

In this example, there is a function that creates in the project folder, files according to the amount passed, following the pattern of arquivo-00 , arquivo-01 , and so on. For each file created, a random password is created, in default ABCDEFG12300 , ABCDEFG12301 and so on.

After creating the files, an authentication function is called, where it is necessary to pass a password as a parameter. When you pass the password, the program reads in loop , the files created, removes the password contained in them and makes the comparison, if the password exists in one of the files, it shows the password and the file that contains the same. / p>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int contArq = 0;

void criarArquivos(int numArq) {
    char senha[20];
    strcpy(senha, "ABCDEFG123");

    for (int i = 0; i < numArq; i++) {
        FILE *arquivo;
        char nomeArq[100];

        sprintf(nomeArq, "arquivo-%02d.txt", i);

        sprintf(senha, "ABCDEFG123%02d", i);

        arquivo = fopen(nomeArq, "w");

        fputs(senha, arquivo);

        if (arquivo != NULL) {
            fclose(arquivo);

            contArq++;
        } else {
            perror(nomeArq);
            exit(EXIT_FAILURE);
        }
    }
}

void autenticarSenha(char senha[]) {
    char linha[100];
    char nomeArq[100];

    for (int i = 0; i < contArq; i++) {
        FILE *arquivo;
        sprintf(nomeArq, "arquivo-%02d.txt", i);

        arquivo = fopen(nomeArq, "r");

        while(fgets(linha, sizeof linha, arquivo) != NULL){
            if(strcmp(linha, senha) == 0){
                printf("Autenticacao concluida, a senha se encontra no arquivo %s", nomeArq);
                printf("\nSenha: %s", linha);

            }
        }

        fclose(arquivo);
    }
}

main(){
    char senha[100];
    strcpy(senha, "ABCDEFG12303");

    criarArquivos(5);

    autenticarSenha(senha);
}
    
03.11.2018 / 15:49