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