I have almost completed the program although I can't FWITE or FREAD my linked lists to and from a file. This is in case 5 and everytime I run it, it returns nothing and closes the program. Does anyone know how to do this??
#include<stdio.h>#include<stdlib.h>#define NUM 1000typedef struct node { int event_year, event_month, event_day, start_time, finish_time; char event_name[100]; struct node *next;}node;int compute_day_month(int date, int month, int year){ int a,m,y,day; date_validation(year,month,date);/*check if dates entered by user are valid*/ a=((14-month)/12); y=(year-a); m=(month+(12*a)-2); if(month==1) printf("\nJan\n"); if(month==2) printf("\nFeb\n"); if(month==3) printf("\nMar\n"); if(month==4) printf("\nApr\n"); if(month==5) printf("\nMay\n"); if(month==6) printf("\nJun\n"); if(month==7) printf("\nJul\n"); if(month==8) printf("\nAug\n"); if(month==9) printf("\nSep\n"); if(month==10) printf("\nOct\n"); if(month==11) printf("\nNov\n"); if(month==12) printf("\nDec\n"); day=(date+y+(y/4)-(y/100)+(y/400)+((31*m)/12))%7; if(day==0) printf("Sunday\n"); if(day==1) printf("Monday\n"); if(day==2) printf("Tuesday\n"); if(day==3) printf("Wednesday\n"); if(day==4) printf("Thursday\n"); if(day==5) printf("Friday\n"); if(day==6) printf("Saturday\n"); return 1;}void display_events(struct node *head){ struct node *nod; for(nod=head; nod!=NULL; nod=nod->next){ printf("%d_%d_%d_%d_%d_%s\n",nod->event_day,nod->event_month,nod->event_year,nod->start_time,nod->finish_time,nod->event_name); printf("\n"); } }node* sequential_search(node *head, int date, int month, int year){ for (;head!=NULL; head=head->next) { if ((head->event_day==date) & (head->event_month==month) & (head->event_year==year)) { compute_day_month(date,month,year); printf("\nStart Time:%d \nFinish Time:%d \n%s\n",head->start_time,head->finish_time,head->event_name); } else printf("No events found, redefine your search criteria"); } return NULL;}int find_structure_events(int scan_year,int scan_month,int scan_day,int chart_start_time,struct node *head){ struct node* nod; int found_count=0; for(nod=head; nod!=NULL; nod=nod->next) { if(scan_year==nod->event_year && scan_month==nod->event_month && scan_day==nod->event_day && (chart_start_time>=nod->start_time && chart_start_time<=nod->finish_time)) found_count=found_count+1; else found_count=0; } return found_count;}struct node* mknode(int eyear, int month, int day, int begin_time, int end_time,char event_desc[50]){ struct node *np; np=(struct node*)malloc(sizeof(struct node)); if(np) { np->event_year=eyear; np->event_month=month; np->event_day=day; np->start_time=begin_time; np->finish_time=end_time; strcpy(np->event_name,event_desc); np->next=NULL; } return np;}struct node* append_node(struct node** head, struct node* np)/*referred from textbook page- 6-6 */{ struct node* nod; if(*head==NULL) *head=np; else { for(nod=*head;nod->next!=NULL;nod=nod->next); /*above for loop has now reach the tail*/ nod->next=np; } return np;}struct text { char diary_events[];};int date_validation(int eyear,int month,int day){ int maximum_date=0; if (month<1 || month>12 || day<0 || eyear<1753) { printf("You have entered an invalid date\n\n\n"); return 0; } else { if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) { maximum_date=31; } if (month==4 ||month==6 ||month==9 ||month==11) { maximum_date=30; } if (month==2) { if((eyear % 4)!=0) { maximum_date=28; } else if((eyear % 400)==0) { maximum_date=29; } else if((eyear % 100)==0) { maximum_date=28; } else { maximum_date=29; } } if(day>maximum_date) { printf("You have entered an invalid date for the event\n"); return 0; } else { return 1; } }}int time_conversion(int y1, int y2){ int hours1,hours0,minutes1,minutes0; struct node Node; Node.start_time=y1; Node.finish_time=y2; hours0=(Node.start_time/100); minutes0=(Node.start_time %100); //if user enters minutes bigger than 60, the time is adjusted appropriately while (minutes0>59 && minutes0<100) { hours0=hours0+1; minutes0=minutes0-60; } hours1=(Node.finish_time/100); minutes1=(Node.finish_time % 100); //if user enters minutes bigger than 60, the time is adjusted appropriately while (minutes1>59 && minutes1<100) { hours1=hours1+1; minutes1=minutes1-60; } printf("Start Time: %d hours %d minutes\n", hours0, minutes0); printf("Finish Time: %d hours %d minutes\n", hours1, minutes1); }int main(void){int temp_word[200]={0}, outbuff[NUM];int menu,eyear,month,day,begin_time,end_time,fail=0,count_events,no_events,rvalue,nread,n;char event_desc[50],in_filename[50]={0},file_event_name[50],output_file_name[30];//int buffer[200];int f_year,f_month,f_day,f_start_time,f_finish_time;struct node *nod;struct node *head=NULL;struct node *backhead;FILE* input_file;FILE* output_events;int t,found_count,scan_year,scan_month,scan_day,chart_time,chart_start_time;int time_array[33]={900,915,930,945,1000,1015,1030,1045,1100,1115,1130,1145,1200,1215,1230,1245,1300,1315,1330,1345,1400,1415,1430,1445,1500,1515,1530,1545,1600,1615,1630,1645,1700};printf("Welcome to the INTERACTIVE USER CALENDAR APPLICATION\n");printf("====================================================\n\n"); do{ /*The do while loop enables the user to continuously use the menu until exiting when they want to do so.*/ printf("\nMENU\n"); printf("====================================================\n\n"); printf("1= Make new Diary Entry\n"); printf("2= Read Diary Entries from text file \n"); printf("3= Display all Diary Entries\n" ); printf("4= Search for Diary Entry\n"); printf("5= Write all Diary Entries to new file\n"); printf("6= Book events to Chart\n"); printf("7= End this session\n\n"); printf("Choose any of the above choices\n");/*Asks the user to choose one of the menu options*/ scanf("%d",&menu); switch(menu) { case 1: printf("Your choice: 1\n\n"); printf("Make new Diary Entry:\n"); printf("==========================================\n"); printf("Enter the number of events you want to enter into the diary\n"); scanf("%d",&no_events); for(count_events=0; count_events<no_events; count_events++) { printf("Event number : %d\n", count_events); printf("Enter day in this format: dd\n"); scanf("%d",&day); printf("Enter month in this format: mm\n"); scanf("%d",&month); printf("Enter year in this format: yyyy\n"); scanf("%d",&eyear); fail=date_validation(eyear,month,day); while (fail!=0) { printf("Enter start time in four digits and in intervals of 00, 15, 30, 45:\n"); scanf("%d",&begin_time); printf("Enter finish time in four digits and in intervals of 00, 15, 30, 45:\n"); scanf("%d",&end_time); time_conversion(begin_time, end_time); //make sure that user enters a finish time larger and not equal to start time if (begin_time>=end_time) {printf("Error:Invalid times entered\n"); return 0;} printf("Describe event in 50 characters(use \"_\" for spaces):\n"); scanf("%s",&event_desc); nod=mknode(eyear,month,day,begin_time,end_time,event_desc); append_node(&head,nod); fail=0; } } break; case 2: printf("Your choice: 2\n\n"); printf("Read Diary Entries from text file: \n"); printf("==========================================\n"); nod=(node*)(malloc(1000*sizeof(struct node))); printf("Enter location of file\n"); scanf("%s",in_filename); input_file=fopen(in_filename,"r"); // If no text file is found, print error message if(input_file==NULL) { printf("Error,no text file found"); return 0; } rvalue=0; rvalue=fscanf(input_file, "%d", &f_year); while (rvalue!=EOF) { rvalue=fscanf(input_file, "%d", &f_month); rvalue=fscanf(input_file, "%d", &f_day); rvalue=fscanf(input_file, "%d", &f_start_time); rvalue=fscanf(input_file, "%d", &f_finish_time); rvalue=fscanf(input_file, "%s", &file_event_name); nod=mknode(f_year,f_month,f_day,f_start_time,f_finish_time,file_event_name); append_node(&head,nod); rvalue=fscanf(input_file,"%d", &f_year); }fclose(input_file); display_events(head);/*displays diary entries on the file and those the user has entered*/ break; case 3: printf("Your choice: 3\n\n"); printf("Display all Diary Entries:\n"); printf("==========================================\n"); printf("Here are all the Diary Entries:\n"); display_events(head); break ; case 4: printf("Your choice: 4\n\n"); printf("Search for a Diary Entry:\n"); printf("==========================================\n"); printf("Enter Date in the format: dd \n"); scanf("%d",&day); printf("Enter Month in the format: mm\n"); scanf("%d",&month); printf("Enter year in this format: yyyy\n"); scanf("%d",&eyear); sequential_search(head,day,month,eyear); break ; case 5: printf("Your choice: 5\n"); printf("Write all Diary Entries to new file: \n"); printf("==========================================\n"); printf("Enter Location of output file\n"); scanf("%s",output_file_name); for(n=0;n<1000; n++) outbuff[n]=n; output_events=fopen(output_file_name,"wb"); fwrite(head,sizeof(struct node),1000,output_events); /* Write to file*/ fclose(output_events); input_file=fopen(output_file_name,"rb");/*open file*/ nread=fread(backhead,sizeof(struct node),1000,input_file); /*read numb of items wrote to file*/ printf(" %d items read from the file\n", nread); return 0; break ; case 6: printf("Your choice: 6\n"); printf("enter the year\n"); scanf("%d",&scan_year); printf("enter the month\n"); scanf("%d",&scan_month); printf("enter the day\n"); scanf("%d",&scan_day); for(t=0;t<33;t++) { chart_start_time=time_array[t]; found_count=find_structure_events(scan_year,scan_month,scan_day,chart_start_time,head); chart_time=(chart_start_time%100); if (chart_time==0) printf("|"); if (found_count==1) printf("#"); else if (found_count>1) printf("x"); else printf("."); } break ; case 7: break ;/*The Program Ends!*/ } }while(menu!=7); return 0;}