Home > database >  Username not written when saving username and password to a file
Username not written when saving username and password to a file

Time:12-25

The following function is for a login system. It works 100% except for the variable "username" (user inputted) writes nothing/blank (loss of data), whereas the variable "password" writes the data perfectly.

void login()
{
   while (1)
   {
       printf("Enter a selection (number 1 or 2), then press enter.\n\n");
       printf("1. Login\n2. Register\n\n");
       
       int selection = 0;
       scanf("%d", &selection);
       if (selection == 1)
       {
           FILE *fp;
           
           char username[24] = {'\0'};
           printf("\nEnter username:\n");
           fgets(username, sizeof(username), stdin);
           
           int c = 0;
           while ((c = getchar()) != '\n' && c != EOF);
           
           char password[24] = {'\0'};
           printf("\nEnter password:\n");
           fgets(password, sizeof(password), stdin);
           
           char extension[4] = ".txt";
           char fileName[strlen(username)   strlen(extension)   1];
           strcpy(fileName, username);
           strcat(fileName, extension);
           
           fp = fopen(fileName, "r");
           
           if (fp != NULL)
           {
               char fileContents1[24] = {'\0'};
               char fileContents2[24] = {'\0'};
               for (int i = 0; i <= 1; i  )
               {
                    if (i == 0)
                    {
                        fgets(fileContents1, sizeof(fileContents1), fp);
                        
                        if (i == 1)
                        {
                            fgets(fileContents2, sizeof(fileContents2), fp);
                        }
                    }
                  
                    if ((username == fileContents1) && (password == fileContents2))
                    {
                        menu();
                    } else
                    {
                        printf("\nInvalid username or password, try again.\n\n");
                        continue;
                    }
               }
            } else 
            {
                printf("\nError, try again.\n\n");
                continue;
            }
           
            fclose(fp);
           
       } else if (selection == 2)
       {
           FILE *fp;
           
           char username[24] = {'\0'};
           printf("\nChoose a username:\n");
           fgets(username, sizeof(username), stdin);
           
           int c = 0;
           while ((c = getchar()) != '\n' && c != EOF);
           
           char password[24] = {'\0'};
           printf("\nChoose a password:\n");
           fgets(password, sizeof(password), stdin);
           
           char extension[4] = ".txt";
           char fileName[strlen(username)   strlen(extension)   1];
           strcpy(fileName, username);
           strcat(fileName, extension);
           
           fp = fopen(fileName, "w");
           
           if (fp != NULL)
           {
               fputs(username, fp);
               fputs(password, fp);
               
               printf("\nLogin created successfully.\n\n");

           } else
           {
               printf("\nError, try again.\n\n");
               continue;
           }
           
           fclose(fp);
       } else
       {
           printf("\nInvalid selection, try again.\n\n");
           continue;
       }
   }
}

CodePudding user response:

At least these problems:

Left over '\n'

scanf("%d", &selection); does not consume anything after the number, like the trailing '\n'.

fgets() reads that '\n' - a very short line.

scanf("%d", &selection);
...
fgets(username, sizeof(username), stdin);

Better to not mix scanf() with fgets(...,..., stdin) usage.

Best to just use fgets().

Remember fgets() reads and saves the final '\n'

You likely want to lop off a potential '\n' after a fgets().

username[strcspn(username, "\n")] = '\0';

Pointer compare

username == fileContents1 compares the addresses of the 2 strings. To compare the content of the strings, use strcmp().

Dubious code

int c = 0; while ((c = getchar()) != '\n' && c != EOF); after a fgets() only makes sense if the fgets() failed to read the entire line. If fgets() did, then this while() reads and tosses the next line.

  • Related