0

My issue is when I print out the value of(*user).username outside of the while loop (still in decryptPass) I get the string it was set to, but if I print it inside of the while loop I get whatever string is at the top of the file. I have tried changing it to user->username which didn't work (unsurprisingly). I'm not interested in changing the way the code works but it's essentially just getting every other string of a text file and if the string in the file is the same as the user inputted string it will set the next string to = key.

struct user
{
    char username[20];
    char encryptedPass[20];
    float balance;
};

void decryptPass(struct user *user, struct user userFile[], FILE *encrypKey, FILE *userDetails);

void main()
{
    FILE *encrypKey;
    FILE *userDetails;
    struct user user;
    struct user userFile[20];
    userDetails = fopen("userDetails.txt", "a+");
    encrypKey = fopen("encryptedKey.txt", "a+");
    printf("Enter username: ");
    scanf("%s", &user.username);
    decryptPass(&user, &userFile[20], encrypKey, userDetails);
    fclose(userDetails);
    fclose(encrypKey);
}

void decryptPass(struct user *user, struct user userFile[], FILE *encrypKey, FILE *userDetails)
{
    char buffer[20], key[5];
    int i = 0;

    while(fgets((userFile[i].username), 20, encrypKey))
    {
        if(strcmp((*user).username, userFile[i].username) == 0)
        {
            fgets(key, 20, encrypKey);
        }
        else
        {
            fgets(buffer, 20, encrypKey);
        }
        i++;
    }
}

If I strcpy the (*user).username to a local variable of the function then use that instead it works fine. I understand this is a scope issue but I'm just not sure why.

  • 2
    The argument `&userFile[20]` of function `decryptPass` is fatally bad because it is out-of-range. Also The `&` in `scanf("%s", &user.username);` should be removed, or *undefined behavior* is invoked by passing data having wrong type (passing `char(*)[20]` while `char*` is expected). – MikeCAT May 16 '21 at 14:34
  • Also `fgets(key, 20, encrypKey);` is bad because you are lying that `key` has 20 elements while there are actually only 5 elements. – MikeCAT May 16 '21 at 14:34
  • Also looks related: [c - strcmp on a line read with fgets - Stack Overflow](https://stackoverflow.com/questions/2404794/strcmp-on-a-line-read-with-fgets) – MikeCAT May 16 '21 at 14:35
  • @MikeCAT could you explain why the `&userFile[20]` is out of range? (not saying it isn't I just don't understand :) ), the problem isn't related to [link](https://stackoverflow.com/questions/2404794/strcmp-on-a-line-read-with-fgets) as my issue is the fact that user.username is getting changed to a string from the file inside of the while loop and is not related to the use of strcmp. Thanks :) – Guy Lawrence-Downs May 17 '21 at 15:18
  • `&userFile[20]` is out-of-range because the array `userFile` has only 20 elements and `&userFile[20]` is requesting a pointer to the 21st element of the array. (Note that indice for arrays in C start from zero) Just getting a pointer to an element one past from the last element is legal, but you mustn't read nor write such element after the last element. The function `decryptPass` actually reads the element that the argument `userFile` points to, so out-of-range access happens here. – MikeCAT May 17 '21 at 15:24
  • @MikeCAT thank you that makes a lot of sense! I thought I had to define the size that's why I had added the [20]. – Guy Lawrence-Downs May 17 '21 at 19:18

0 Answers0