Added 'create_user_list' function

master
Aadhavan Srinivasan 2 years ago
parent c0c567c7ae
commit 2492678652

@ -6,34 +6,40 @@
int num_of_lines(char* filename);
char* file_to_string(char* filename);
User* create_user_list(char* filename);
int main() {
int num_users = num_of_lines("user_file.txt");
/* Structure of user file - <username> <password> on every line */
char* file_str = file_to_string("user_file.txt");
User* users = create_user_list("user_file.txt");
char* token = malloc(sizeof(char) * 30);
User users[num_users];
Message message = new_message("Hello, this is a text message",users[0],users[1]);
token = strtok(file_str," \r\n");
printf("Message was: %s\nSentfrom: %s\nSent to: %s\nWith password: %s\nSent at: %s\n",message.text, message.sender.username, message.recipient.username, message.recipient.password, asctime(&message.timeinfo));
for (int i=0;i<num_users;i++) {
users[i].username = token;
token = strtok(NULL," \r\n");
users[i].password = token;
token = strtok(NULL," \r\n");
}
}
Message message = new_message("Hello, this is a text message",users[0],users[1]);
printf("Message was: %s\nSentfrom: %s\nSent to: %s\nSent at: %s\n",message.text,message.sender.username,message.recipient.username,asctime(&message.timeinfo));
}
User* create_user_list(char* filename) {
/* Structure of user file - <username> <password> on every line */
int num_users = num_of_lines(filename);
char* file_str = file_to_string(filename);
char* token = malloc(sizeof(char) * 30);
User* users = calloc(num_users, sizeof(User));
token = strtok(file_str," \r\n");
for (int i=0;i<num_users;i++) {
users[i].username = token;
token = strtok(NULL," \r\n");
users[i].password = token;
token = strtok(NULL," \r\n");
}
return users;
}

Loading…
Cancel
Save