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