Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185435
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 9, 20222022-06-09T22:26:31+00:00 2022-06-09T22:26:31+00:00

How read a certain string from a file? [C]

  • 0

[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]

  • 0 0 Answers
  • 9 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 401 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 371 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 367 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.