I need to have an auth in a .csv file, mi users.csv
file looks like this:
1,root,root,1
2,user,user,0
The columns represents [id,user,pass,isAdmin]
What I need is to search in the .csv rows in the columns [user, pass] to check if it's equal to the user and pass entered by the user, btw, I'm using an struct in this way
My code
struct User {
int id;
char user[20];
char password[20];
bool isAdmin;
};
And a function in this way:
void getUser (struct User *currentUser, FILE *users) {
char allUsers[100][20];
char user[20];
char password[20];
printf("Gimme your username: ");
scanf("%s", user);
printf("Gimme your password: ");
scanf("%s", password);
char line[MAX_USER_ROW_LENGTH];
while (fgets(line, sizeof(line), users)) {
char *token = strtok(line, DELIMITER);
char *token2 = strtok(line, DELIMITER);
token2 = strtok(NULL, DELIMITER);
while (token != NULL) {
if ((strcmp(user, token) == 0) && strcmp(password, token2) == 0) {
printf("user an pass correct");
/**
* here goes the code that initialize the user, i plan to do something like, if the user and pass
* are correct i would do something like this:
* currentUser->id = id;
* currentUser->user = user;
* currentUser->password = password;
* currentUser->isAdmin = 1 or 0
*
* I just relize that i have no idea how to initialize the user, sorry :c
* */
} else {
printf("user and pass not found");
return;
}
token = strtok(NULL, DELIMITER);
token2 = strtok(NULL, DELIMITER);
}
}
}
I've tried
I've used the function strtok()
to separate each row between the commas, but, I can only compare the username or the password, not twice at each iteration, (sorry if the wile is empty, every ting i've tried, i did ctrl+z because i entered in panic).
Once i've checked if the user exists and can in, i should pass the values to the *currentUser
, but i think i can do it.
Thank you if you can help me.