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);
}
Welcome to Stackoverflow Paulo. Please post an excerpt of the code you already have to get a better idea of the problem, I suggest you read this article: How to create a Minimum, Complete and Verifiable example. And, correct tags, remove tag
c++
, if the program is inc
even.– Pedro Gaspar