My program to reads two strings from a .txt file and compares it to two strings entered by the user. The first string entered is usuario (username) and the second contrasenia (password). The content of the file is saved in this way:
david
Contrasenia1
Eduardo
Newlabel21
The first and third string are the username with their respective password below.
I don't encrypt the file because I want to solve this errors first.
I adapted the program to normal C because in Replit you can't use system("pause")
and system("cls")
.
iniciarSesion.h :
#define maxIntentos 3
void iniciarSesion();
iniciarSesion.c :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iniciarSesion.h"
void iniciarSesion() {
FILE *docInicioSesion;
char usuario[40], contrasenia[30];
int tries = 0, ingreso = 0;
docInicioSesion = fopen("saveData.txt", "r");
if (docInicioSesion == NULL) {
printf("\nError al abrir el archivo\n");
}else {
do {
system("cls");
printf("\n\t\t INICIO DE SESION\n\n");
printf("Usuario: ");
scanf("%s", usuario);
printf("Contrasena: ");
scanf("%20s", contrasenia);
char stringUser[strlen(usuario)], stringPassword[strlen(contrasenia)];
while(!feof(docInicioSesion)) {
fseek(docInicioSesion, 0, SEEK_SET);
fgets(stringUser, strlen(usuario), docInicioSesion);
fgets(stringPassword, strlen(contrasenia), docInicioSesion);
}
if(strcmp(usuario, stringUser) && strcmp(contrasenia, stringPassword)) {
system("cls");
printf("\nAcceso Concedido!\n");
printf("Bienvenido/a %s!\n", usuario);
system("pause");
ingreso = 1;
}else {
printf("Acceso denegado\nLa clave y/o usuario son
incorrectos...\n");
printf("%s\n", stringPassword);
printf("%s", stringUser);//I wrote this to check what it's happening
tries++;
}
}while(tries<maxIntentos || ingreso !=1);
if( tries == maxIntentos) {
printf("Ha sobrepasado el limite maximo de intentos.\n");
system("pause");
system("cls");
system("exit");
}
}
fclose(docInicioSesion);
}
I can only get the false section of the if statement and sometimes print:
Acceso denegado\nLa clave y/o usuario son incorrectos...
d
davidC
Sometimes because it depends of the string entered.