The text file "mh.txt" includes the following names, surnames, gender and birth date of 3 people separated by semicolons.
which look like this:
Tiffany;Evans Smith;F;22/01/1989;
Alex;Williams;M;23/06/1988;
Clay;Bristol;F;30/12/1989;
I want to store every person's details in a node so we'd have 3 nodes in total. All connected in a linked list. Then I want to print the linked list using a print function.
The problem is that my compiler displays the message "fix.exe has stopped working" every time I compile and run! my c file is called fix.c
This is my attempt:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
#define SIZE 100
struct Node{
char fname[SIZE];
char sname[SIZE];
char gender;
char Byear[SIZE];
struct Node *next;
};
struct ListRecord
{
struct Node *head;
struct Node *tail;
int size;
};
typedef struct ListRecord List;
List* CreateList(void);
void MakeEmptyList(List *);
void printFriends(List *);
List * initialiseFL(List *);
int main(){
List *myList;
int option;
int exit=FALSE;
char fname[SIZE];
myList = CreateList();
myList=initialiseFL(myList);//reads data from text file and adds nodes
to linked list
while(!exit){
fflush(stdin);
printf("C:>FriendBook friends.txt\n");
printf("Your FriendBook has been created.\n");
printf("(1) Insert a new friend\n");
printf("(2) Print your friends\n");
printf("(3) Search for your friend\n");
printf("(4) Block your friend\n");
printf("(5) Print your blocked friend\n");
printf("(6) Exit\n");
printf("Enter your option:");
printf("\n\n");
scanf("%d",&option);
fflush(stdin);
switch(option){
case 2:
printFriends(myList);
break;
default:
printf("Command not recognised!!\n");
break;
}
}
return 0;
}
List* CreateList(){
List *l;
l = (struct ListRecord *) malloc(sizeof(struct ListRecord));
if (l == NULL)
printf("Out of memory!\n");
MakeEmptyList(l);
return l;
}
void MakeEmptyList(List *l)
{
l->head = (struct Node *) malloc(sizeof(struct Node));
if (l->head == NULL)
printf("Out of memory!\n");
l->head->next = NULL;
l->tail = l->head;
l->size = 0;
}
List* initialiseFL(List *l){
FILE *IN;
IN =fopen("mh.txt", "r");
if (IN == NULL){
printf("Could not open text file!!\n");
}
while((fgetc(IN))!=EOF){
struct Node *p;
p=(struct Node*) malloc(sizeof(struct Node));
char temp;
temp=fgetc(IN);
int i=0;
//read first name a char at a time until ;
while(temp!=';'){
p->fname[i]=temp;
i++;
temp=fgetc(IN);
}
p->fname[i]='\0';
//read second name a char at a time until ;
temp=fgetc(IN);
i=0;
while(temp!=';'){
p->sname[i]=temp;
i++;
temp=fgetc(IN);
}
p->sname[i]='\0';
//read gender from text file
temp=fgetc(IN);
p->gender=temp;
//read birth year from text file
temp=fgetc(IN);
temp=fgetc(IN);
i=0;
while(temp!=';'){
p->Byear[i]=temp;
i++;
temp=fgetc(IN);
}
p->Byear[i]='\0';
//if list is empty, add node right after the dummy
if (l->size==0){
l->head->next=p;
l->tail=p;
l->size++;
}
//if the list contanins one node after the dummy
struct Node *first;
first=l->head->next;
if(l->size==1){
if(strcmp(p->fname[0],first->fname[0])>0 && first->next==NULL){
p->next=NULL;
first->next=p;
l->tail=p;
l->size++;
}
else{
p->next=first;
l->head->next=p;
l->size++;
first=l->head->next;//reset "first" to point to the 1st
node in the list
}
}
struct Node *second;
second=first->next;
//if it is to be added at the beginning
if(l->size>=2){
if(strcmp(p->fname[0],first->fname[0])<0){
p->next=first;
l->head->next=p;
l->size++;
first=l->head->next;//reset "first" to point to the 1st node
in the list
}
else{
while(!(strcmp(p->fname,first->fname)>0 && strcmp(p-
>fname,second->fname)<0 )){
first=first->next;
second=second->next;
}
if(second->next==NULL){
p->next=NULL;
first->next=p;
}
else{
p->next=second;
first->next=p;
}
first=l->head->next;//reset "first" to point to the 1st node
in the list
second=first->next;//reset "second" to point to the 2st node
in the list
}
}
}
fclose(IN);
return l;
}
void printFriends(List *l){
//make a temp head that points to the dummy
struct Node *temp;
temp=(struct Node*) malloc(sizeof(struct Node));
temp=l->head->next;
printf("Your friends are listed below.\n");
printf("Name\tSurname\tGender\tBirth Year\n");
while(temp!=NULL){
printf("%s\t%s\t%c\t%s\n",temp->fname,temp->sname,temp-
>gender,temp->Byear);
temp=temp->next;
}
}