Home > Blockchain >  Cannot open a file to read login data from in C
Cannot open a file to read login data from in C

Time:01-03

This function is for a login system in C. It launches correctly and is seemingly proper code according to language syntax. When I try to open the file after entering username/password, it returns the "Error, try again" message. The file is a .txt file with two lines, the username and password.

void login()
{
   while (1)
   {
       printf("---- Bank Management ----\n\n");
       printf("Please login...\n");
       
       char username[25] = {'\0'};
       printf("\nEnter username:\n");
       fgets(username, sizeof(username), stdin);
       
       int c = 0;
       while (c = getchar() != '\n' && c != EOF);
           
       char password[25] = {'\0'};
       printf("\nEnter password:\n");
       fgets(password, sizeof(password), stdin);
           
       char extension[5] = ".txt";
       char fileName[31] = {'\0'};
       strcpy(fileName, username);
       strcat(fileName, extension);
       
       FILE *fp = fopen(fileName, "r");
           
       if (fp != NULL)
       {
           char setUsername[5] = "bank";
           char setPassword[5] = "temp";
           char fileContents1[25] = {'\0'};
           char fileContents2[25] = {'\0'};
               
           for (int i = 0; i <= 2; i  )
           {
                if (i == 1)
                {
                    fgets(fileContents1, sizeof(fileContents1), fp);
                        
                    if (i == 2)
                    {
                        fgets(fileContents2, sizeof(fileContents2), fp);
                    }
                }
                  
                if ((strcmp(setUsername, fileContents1) != 0) && (strcmp(setPassword, fileContents2) != 0))
                {
                    menu();
                } else
                {
                    printf("\nInvalid username or password, try again.\n\n");
                    continue;
                }
            }
        } else
        {
            printf("\nError, try again.\n\n");
            continue;
        }
           
        fclose(fp); 
   }
}

CodePudding user response:

Instead of wrapping all your code in if( fp != NULL ), check for the error and bail out immediately.

FILE *fp = fopen(fileName, "r");
if( fp == NULL ) {
  perror(fileName);
}

// Without a size, C will figure it out.
char setUsername[] = "bank";
char setPassword[] = "temp";
...

Use perror to a decent error message with a reason.

myusername
.txt: No such file or directory.

Whoops, the username has a newline.


fgets reads the whole line, including the newline. That newline has to be stripped off.

That might be what while (c = getchar() != '\n' && c != EOF); is trying to do, but it isn't. It's reading input until it sees a newline or end-of-file... and throwing the input out. That's why the user has to hit enter twice after entering their username.

fgets has already read the username from input into memory. We can write a little function to jump to the last character of the string and turn the newline into a null.

void strip_newline(char *string) {
    char *end = string   strlen(string) - 1;
    if( *end == '\n' ) {
        *end = '\0';
    }
}

Or we can use scanf because it strips trailing whitespace.

// "" is equivalent to {'\0'} and easier to understand.
char username[25] = "";
printf("\nEnter username:\n");
// Unlike fgets, scanf does not leave space for the null byte.
scanf("$s", username);

char password[25] = "";
printf("\nEnter password:\n");
scanf("$s", password);

Normally scanf is to be avoided, but its fine for this simple parsing.

CodePudding user response:

regarding:

fgets(...) statements. 

when reading input, the '\n' is included in the data read.

So must remove that char.

  • Related