-1

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.

user4157124
  • 2,809
  • 13
  • 27
  • 42
DaSe
  • 1
  • 3
  • There are quite a few issues in the code. But one immediate one is that `strcmp` returns 0 on match. You are checking for the opposite. Please read the [strcmp manual](https://man7.org/linux/man-pages/man3/strcmp.3.html). – kaylum Jun 09 '22 at 22:25
  • `char stringUser[strlen(usuario)]` This is also a very bad idea. You should read entire lines from the file and not just the same size as what the user enters. Otherwise you will need to deal with partially read lines. – kaylum Jun 09 '22 at 22:28
  • You will want to look at [Why is while ( !feof (file) ) always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) .. – David C. Rankin Aug 30 '22 at 20:48

1 Answers1

1

strcmp returns 0 when strings are equal, so line if(strcmp(usuario, stringUser) && strcmp(contrasenia, stringPassword)) probably should be corrected to something like:

if(strcmp(usuario, stringUser) == 0 && strcmp(contrasenia, stringPassword) == 0)
gizlu
  • 86
  • 5
  • Yet another example of why `strcmp` should've been named or at least aliased as something like `strdiff` in the standard C library. – mtraceur Aug 30 '22 at 19:25