First commit
commit
b048b09f64
@ -0,0 +1,46 @@
|
||||
#include "message.h"
|
||||
#include "user.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
User[num_of_lines("user_file.txt")] users;
|
||||
|
||||
FILE* fp = fopen("user_file.txt", "w+");
|
||||
|
||||
|
||||
Message message = new_message("Hello, this is a text message",user1,user2);
|
||||
|
||||
printf("Message was: %s\nSentfrom: %s\nSent to: %s\nSent at: %s\n",message.text,message.sender.username,message.recipient.username,asctime(&message.timeinfo));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Finds the number of lines in a file (Which MUST end in a new-line, if the last
|
||||
line is to be counted) */
|
||||
|
||||
int num_of_lines(char* filename) {
|
||||
|
||||
int num_lines = 0;
|
||||
FILE* fp = fopen(filename,"r");
|
||||
|
||||
if (fp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
for (int c = getc(fp); c != EOF; c = getc(fp)) {
|
||||
if (c == '\n') {
|
||||
num_lines++;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return num_lines;
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "message.h"
|
||||
|
||||
Message new_message(char* string, User from, User to) {
|
||||
Message new_message;
|
||||
|
||||
new_message.text = malloc((strlen(string)+1)*sizeof(char));
|
||||
strcpy(new_message.text,string);
|
||||
|
||||
new_message.sender = from;
|
||||
new_message.recipient = to;
|
||||
|
||||
time_t rawtime;
|
||||
struct tm timeinfo;
|
||||
|
||||
time(&rawtime);
|
||||
timeinfo = *localtime(&rawtime);
|
||||
|
||||
new_message.timeinfo = timeinfo;
|
||||
|
||||
return new_message;
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
|
||||
|
||||
#ifndef _MESSAGE_H
|
||||
#define _MESSAGE_H
|
||||
|
||||
#include "user.h"
|
||||
#include <time.h>
|
||||
|
||||
typedef struct Message_s Message;
|
||||
|
||||
struct Message_s {
|
||||
char* text;
|
||||
struct tm timeinfo;
|
||||
User sender;
|
||||
User recipient;
|
||||
};
|
||||
|
||||
Message new_message(char* text,User from,User to);
|
||||
|
||||
#endif
|
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "user.h"
|
||||
|
||||
User new_user(char* name,char* pass) {
|
||||
User newUser;
|
||||
|
||||
newUser.username = malloc((strlen(name)+1)*sizeof(char));
|
||||
|
||||
newUser.password = malloc((strlen(pass)+1)*sizeof(char));
|
||||
|
||||
|
||||
strcpy(newUser.username,name);
|
||||
strcpy(newUser.password,pass);
|
||||
|
||||
return newUser;
|
||||
}
|
Loading…
Reference in New Issue