I'm doing a program for a university project. currently I'm doing the user configuration for being able to change both the password and the user name. I was thinking a way to use a single function for both process instead of creating two separated ones.
My goal was to pass a reference to the struct that holds the user data, and a reference to the corresponding member of the struct I want to edit, both being a char[64]
. So after I edit the member I can just rewrite the whole line in the file with the updated data.
void change_user_data(char *input, struct user ***logged_user,
char *data_to_alter, FILE **user_registry) {
// making sure the returned input is valid.
if (strcmp(input, "..."))
return;
struct user find_user;
fseek(*user_registry, 0, SEEK_SET);
while (!feof(*user_registry)) {
fread(&find_user, sizeof(struct user), 1, *user_registry);
if (0 != strcmp(find_user.user_name, (**logged_user)->user_name))
continue;
strcpy(data_to_alter, input);
fseek(*user_registry, 0, SEEK_CUR - 1);
fwrite((**logged_user), sizeof(struct user), 1, *user_registry);
break;
}
return;
}
This is the function that should handle the change of both the struct and the file.
change_user_data(change_password(input), &logged_user, (*logged_user)->password, &user_id);
and here was my attempt to pass the arguments.
There was no error message, but there wasn't any change neither for the struct or the file. I guess that what is happening is that it's only creating a copy of the member, I tried to change the way I passed the member but none of them worked.
Minimal Reproductable Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 64
struct user {
char user_name[MAXLEN];
char password[MAXLEN];
};
void placeholder_function(struct user *logged_user);
void change_user_data(char *input, struct user **logged_user,
char *data_to_alter, FILE **user_registry);
int main() {
struct user logged_user;
placeholder_function(&logged_user);
return 0;
}
void placeholder_function(struct user *logged_user) {
FILE *user_registry;
if (!(user_registry = fopen("user_registry.bin", "w+b"))) {
printf("Couldn't open file\n");
exit(1);
}
strcpy(logged_user->user_name, "Admin\0");
strcpy(logged_user->password, "Admin\0");
fseek(user_registry, 0, SEEK_SET);
fwrite(logged_user, sizeof(struct user), 1, user_registry);
// expected output: Admin Admin
printf("%s %s\n", logged_user->user_name, logged_user->password);
// rest of program here...
change_user_data("1234\0", &logged_user, logged_user->password,
&user_registry);
printf("%s %s\n", logged_user->user_name, logged_user->password);
// wanted output: Admin 1234
}
void change_user_data(char *input, struct user **logged_user,
char *data_to_alter, FILE **user_registry) {
struct user find_user;
fseek(*user_registry, 0, SEEK_SET);
while (!feof(*user_registry)) {
fread(&find_user, sizeof(struct user), 1, *user_registry);
if (0 != strcmp(find_user.user_name, (*logged_user)->user_name))
continue;
strcpy(data_to_alter, input);
fseek(*user_registry, 0, SEEK_CUR - 1);
fwrite((*logged_user), sizeof(struct user), 1, *user_registry);
break;
}
}