[ad_1]
Hiii
I’m doing a program to read two strings from a .txt file and compare it with other two strings entered by the user. The first string entered by the user had named 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.
Note: For the moment I don’t encrypt the file because I want to solve this errors first.
I’m using Replit to do this program, for that reason I adapted the program to a normal C. 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);
}
When I was running the program 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
I write sometimes because the result depends of the string entered at the beginning of the program.
[ad_2]