0

I am a beginner in c programming. While I figured out how to read the content from my "credentials.txt" file, I cannot figure out how I can compare the username and password user enters with the contents in the text file. Also, any suggestions would be a welcome change.

Sign-Up code (saves username and password to a file):

#include <stdio.h>
#include <string.h>

int main()
{
    char username[19], password[19];

cred_enter_username:
    printf("Enter username (Max 18 characters): ");
    int len_u = strlen(gets(username));

    for (int i = 0; username[i] != '\0'; i++)
    {
        if (username[i] == ' ')
        {
            printf("No spaces allowed. Use only '_' or '-'. Try again\n");
            goto cred_enter_username;
        }
    }

    if (len_u > sizeof(username))
    {
        printf("\n");
        printf("Number of characters exceed allowed characters. Please try again");
        printf("\n\n");
        goto cred_enter_username;
    }

cred_enter_password:
    printf("Enter password (Max 18 characters): ");
    int len_p = strlen(gets(password));

    for (int i = 0; password[i] != '\0'; i++)
    {
        if (password[i] == ' ')
        {
            printf("No spaces allowed. Try again\n");
            goto cred_enter_password;
        }
    }

    if (len_p > sizeof(password))
    {
        printf("\n");
        printf("Number of characters exceed allowed characters. Please try again");
        printf("\n\n");
        goto cred_enter_password;
    }

    FILE *fptr = NULL;
    fptr = fopen("credentials.txt", "a");

    fprintf(fptr, "Username: %s\n", username);
    fprintf(fptr, "Password: %s\n", password);
    fprintf(fptr, "\n");

    fclose(fptr);

    return 0;
}

Log-In code (so far):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    FILE *credentials;
    char *buffer;
    long numbytes;
    char username[19], password[19];
    char userstr[30], passstr[30];

    credentials = fopen("credentials.txt", "r");
    if (credentials == NULL)
    {
        printf("No account found. Sign up first");
        return 1;
    }

    fseek(credentials, 0L, SEEK_END);
    numbytes = ftell(credentials);

    fseek(credentials, 0L, SEEK_SET);

    buffer = (char *)calloc(numbytes, sizeof(char));

    if (buffer == NULL)
    {
        printf("Memory Error");
        return 1;
    }

    fread(buffer, sizeof(char), numbytes, credentials);

cred_enter_username:
    printf("Enter username (Max 18 characters): ");
    int len_u = strlen(gets(username));

    for (int i = 0; username[i] != '\0'; i++)
    {
        if (username[i] == ' ')
        {
            printf("Invalid username. Try again\n");
            goto cred_enter_username;
        }
    }

    if (len_u > sizeof(username))
    {
        printf("\n");
        printf("Number of characters exceed allowed characters. Please try again");
        printf("\n\n");
        goto cred_enter_username;
    }
    
cred_enter_password:
    printf("Enter password (Max 18 characters): ");
    int len_p = strlen(gets(password));

    for (int i = 0; password[i] != '\0'; i++)
    {
        if (password[i] == ' ')
        {
            printf("Invalid. Try again\n");
            goto cred_enter_password;
        }
    }

    if (len_p > sizeof(password))
    {
        printf("\n");
        printf("Invalid password. Try again");
        printf("\n\n");
        goto cred_enter_password;
    }

    return 0;
}
  • 2
    For starters, __never__ use `gets`, especially for security-related tasks. – DYZ Aug 03 '21 at 04:34
  • Okay then what shall I use in place of `gets`? – Dev Shah Aug 03 '21 at 04:37
  • 3
    `fgets(buf, sizeof(buf), stdin)` is a less-insecure replacement for `gets()` – Jeremy Friesner Aug 03 '21 at 04:46
  • 2
    See [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for alternative ways to read whole lines without using `gets()`. Note that most of them include the newline in the string; you'll need to zap that. – Jonathan Leffler Aug 03 '21 at 04:53

2 Answers2

0

Firstly You can read data from the text file line by line and store it in 2 separate string variables. ie. 1)Username 2)Password.

next up, simply compare the username_input with the Username, same for the Password.

im3dabasia
  • 48
  • 10
  • For getting input from the STDIN you can use scanf instead of gets. It is advisable to use scanf since both the username and password are 1 word inputs. – im3dabasia Aug 03 '21 at 04:44
  • Hey thanks, could you tell me how I can read text line by line from the file? – Dev Shah Aug 03 '21 at 04:45
  • You can check this link: https://www.programiz.com/c-programming/examples/read-file – im3dabasia Aug 03 '21 at 04:49
  • 1
    Note that `scanf()` will not require the two words to be on a single line. You could have the username on one line, 4 blank lines, and then the password. You could also have multiple username/password pairs on a single line. Basically, it is not sensible to use `scanf()` (or `fscanf()`) for processing lines. OTOH, using `fgets()` or POSIX `getline()` to read a line and then using `sscanf()` to parse it is often a good way to go. It also makes error reporting easier, in general. – Jonathan Leffler Aug 03 '21 at 04:57
0

If your question is about comparing the username and password from user and your credentials.txt, then instead of storing your username and password in two different line, store them in a single line, separated from other users through a new line, like

user1,pwd1 \n
user2,pwd2 \n
user3,pwd3 \n

then read through the file and store each line(eg: user1,pwd1) to a string and use comma as string delimiter and compare with user input.

ABINESH
  • 43
  • 5