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.