want to read more than 1 file . txt in C

Asked

Viewed 112 times

1

I wanted to create several files. txt and I did it through a loop, and I got it. But I group the only way I can read all the files is by making another loop, example: makes a looping of 3 passwords, ie I created 3 passwords, 3 files . txt, but at login time I don’t want to have to put the 3 passwords, I want my program to read the files and check if the only password to put in the login matches one of the password already saved by the loop.

2 answers

1

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

The solution to your question would be to manipulate a file at a time, as you will do it may be in several ways, but the smartest way would be a vector of files.

  • I understood more or less what you said, if I put here the codes you try to clarify please

  • Yes Paul, put the codes that I will help as much as possible.

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 the pattern ABCDEFG12300, ABCDEFG12301 and so on.

After creating the files, an authentication function is called, where you need to pass a password as a parameter. When passing the password, the program reads in a loop, created files, 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 containing the same.

#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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.