Hi,
Can someone help me to Write a C program that will do the following:
• Read in a number of records of your friends’ names and birthdays (string with format <mmdd>). Save it on a binary file. Each record (struct) must also have a record number (sequential and starting at 1) that is generated automatically by the program and stored in the first field of the struct.
• Create two index files, i.e. a file consisting of a name field and a record number field. The file must be sorted (use a sorting algorithm) according to the name field. Do the same for the birthday field. This process must take place automatically after the names have been entered.
• Use the index files to find the birthday of a friend if the name is entered, or the name if a date is entered, or a list of every birthday in a specific month.
• You must also be able to update the file (go to the end and add records).
• All the above options must be displayed as a menu and implemented by means of appropriate functions. Also give appropriate error messages (e.g. if the file does not exist.
Here is my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define NAME_SIZE 20
#define STRSZ 6
#define MAX_FRIENDS 4
typedef struct {
char name[NAME_SIZE];
char birthday[STRSZ];
} friend_t;
typedef struct s {
struct s *next_record;
friend_t friend;
}record_t;
record_t *friends_start =NULL, *friends_last = NULL;
int read_friend_record(friend_t *friend);
int store_friend_record(const friend_t friend);
friend_t next_friend(const int first);
int friend_name(const friend_t friend, const char name[]);
int save_friend_file(const char file_name[]);
int string_read(const char prompt[], char string[], const int size);
int find_friend(friend_t *friend, char myfriend_name[]);
void print_friend_record(const friend_t friend);
void print_all_friend(void);
int main()
{
friend_t friend;
char myfriend_name[NAME_SIZE];
char filename[NAME_SIZE];
FILE *out;
while(read_friend_record(&friend)) {
if(store_friend_record(friend))
printf("\n Record on '%s' stored ", friend.name);
else
printf("\n\a Failed to store '%s' ", friend.name);
}
printf("\n\n Enter name of file to save friend records ");
scanf("%s", filename);
out = fopen(filename, "wb");
if(out==NULL)
printf("\n Cannot open file for output");
if(save_friend_file(filename))
printf("\n Record on '%s' saved ", friend.name);
else
printf("\n\a Failed to save '%s' \n", friend.name);
fclose(out);
while(string_read("\n\nFind friend name?", myfriend_name, NAME_SIZE)) {
if(find_friend(&friend, myfriend_name))
printf("\n '%s' data found", myfriend_name);
else
printf("\n '%s' data not found", myfriend_name);
}
print_all_friend();
return 0;
}
int read_friend_record(friend_t *friend)
{
printf("\n\nEnter record on next friend \n");
if(string_read("friend name?", (*friend).name, NAME_SIZE))
if(string_read("friend birthday?", (*friend).birthday, STRSZ))
return 1;
return 0;
}
int store_friend_record(const friend_t friend)
{
int num_of_friends = 0;
friend_t friends[MAX_FRIENDS];
record_t *new_record;
if((new_record = (record_t*) calloc(sizeof(record_t), 1)) != NULL)
{
if(friends_start==NULL)
friends_start = new_record;
else
friends_last->next_record = new_record;
friends_last = new_record;
new_record->next_record = NULL;
}
else
{
printf("\n\a creation of new friend record failed \n ");
return 0;
};
new_record->friend = friend;
return 1;
}
friend_t next_friend(const int first)
{
record_t *friends = NULL;
friend_t friend = {""};
if(first)
friends = friends_start;
if(friends != NULL)
{
friend = friends->friend;
friends = friends->next_record;
}
return friend;
}
int friend_name(const friend_t friend, const char name[])
{
return(!strcmp(friend.name, name));
}
int save_friend_file(const char file_name[])
{
FILE *out;
friend_t friend;
printf("\n\nSave friend records to file'%s' ", file_name);
if((out = fopen(file_name, "wb"))==NULL)
printf("\nUnable to open friend records file");
friend = next_friend(1);
while(!friend_name(friend, "")) {
fwrite(&friend, sizeof(MAX_FRIENDS), 1, out);
friend = next_friend(0);
}
fclose(out);
return 1;
}
int string_read(const char prompt[], char string[], const int size)
{
int index = 0, ch;
printf("%s", prompt);
while(((ch=getchar()) !='\n') && (ch!=EOF)) {
if(isprint(ch) && (index<size-1))
string[index++] = (char) ch;
string[index] = '\0';
}
return index;
}
int find_friend(friend_t *friend, char myfriend_name[])
{
*friend = next_friend(1);
while(!friend_name(*friend, "")) {
if(friend_name(*friend, myfriend_name))
return 1;
*friend = next_friend(0);
}
return 0;
}
void print_friend_record(const friend_t friend)
{
printf("\n %-*s %-*s ", NAME_SIZE, friend.name, STRSZ, friend.birthday);
}
void print_all_friend(void)
{
friend_t friend;
printf("\n\n All friends record\n %-*s %-*s ", NAME_SIZE, friend.name, STRSZ, friend.birthday);
friend = next_friend(1);
while(!friend_name(friend, ""))
{
print_friend_record(friend);
friend = next_friend(0);\
}
}