1

I am asked to implement a calendar using structures and files. I succeeded in implementing it using structures. However, I have to build on top of my program in order to handle files as well. The main idea that seemed challenging is how can I write to a text and binary files without overwriting the previous data. Here are some specifications that I am given:

  1. saving the calendar to a text file
  2. saving the calendar to a binary file
  3. reading the calendar from a text
  4. saving the calendar to binary file

Another addition to your program is that of logging to a text file changes you have made to your calendar since you last read it in. Here you should use a file format that would allow you to reload the original calendar and reload the logged changes to recapture the state of the calendar if your program crashes so you lose very little. You should implement this in two ways:

  1. As an explicit menu option, to log all the changes to a file called "eventlog_time.txt", where time is obtained from the "time.h" library.
  2. As an automatic logging action. The program keeps a file called "eventlog_private.txt" that it automatically updates every time the user adds or deletes an event. You will need to initialize this file to empty, then open it, append to it, and close it after logging each event.

Here is the code that I have written:

Can Anyone please explain to me the last part of the specification, because I am not sure about what I am supposed to do, or at least give me a hint in order to start the problem. Here is my code:

        /************************************************************************************************
    Input file: None
    Output file: None
    Description: This calendar program is mainly a menu interactive program that prompts
                 the user for a date and its events, then the entry is stored in its appropriate
                 place inside an array of structures.
                 This program handles 5 main operations:
                    . Add an event to the calendar.
                    . Display events for a specific calendar date.
                    . Display the whole calendar.
                    . Delete the event from the calendar.

    ************************************************************************************************/


    #include <stdio.h>
    #include <string.h>
    #define MAXEVS 100   // Defining the maximum number of events as being 100
    typedef enum {FALSE = 0, TRUE = 1} BOOLEAN;
    typedef struct {
        int hours,
            minutes,
            seconds;
    } time_t;

    typedef struct {
        char month [20];
        int day,
            year; 
    } date_t;

    typedef struct {
        char ev_name [40];
        date_t dt;
        time_t tm;
    } event_t;

    typedef struct {
        BOOLEAN valid;
        event_t event_list [MAXEVS];
        int num_events;
    } calentry_t; 

        int counter = 0; //Global counter variable used to keep track of number of events
        void menu (); // This function displays the menu to the user 
        void InitializeCalender(calentry_t calendar[][31]  ); // Initializes the calendar for valid and invalid dates
        int ReadEvent ( calentry_t calendar[][31],char eventname[], char month[], int *day, int *year, int *h, int *min); // This function min role is to get the input from the user (the whole calendar entry)
        int MonthStr2Num ( char* month); // This function converts a string containing a month to its appropriate number
        void AddEvent (calentry_t calendar[][31], char eventname[], int* monthnum, int *day, int *year, int *h, int *min); // This function places the calendar entry places the event inside its appropriate place inside the 2D array
        char* MonthNum2Str (int* m); // Converts a number to its appropriate month
        void DisplayCalendar (calentry_t calendar[][31], int* monthnum, int* day, int* year); // Displays the calendar entry for a specific date
        void DisplayWholeCalendar ( calentry_t calendar[][31]); // This function displays the whole entries stored inside the calendar
        int DeleteEvent (calentry_t calendar [][31]); // This function deletes an event from the calendar
    main (){
        int choice;
        calentry_t calendar[12][31]; // Declaring a 2D array of structure calentry_t

        char eventname [100];
        char month[20];
        int day, year, h, min, monthnum;
        char temp;

        InitializeCalender(calendar);
        do{ // loop that continues to execute the program until the user decides to quit
        menu ();

        scanf ("%d",&choice);
        switch (choice)
        {
            case 1:
            printf("Calendar will now quit.\n");
            break;
            case 2:
            monthnum = ReadEvent(calendar ,eventname, month, &day, &year, &h, &min);
            AddEvent (calendar, eventname, &monthnum, &day, &year, &h, &min);
            break;
            case 3:
            printf ("What is the date that you want to display its events? input like ( 12 January 2012 ) ");
            scanf ("%d %s %d", &day, month, &year);
            monthnum = MonthStr2Num (month); 
            DisplayCalendar (calendar ,&monthnum, &day, &year);
            break;
            case 4:
            printf ("The whole calendar will be displayed immediately\n");
            DisplayWholeCalendar (calendar);
            break;
            case 5: 
            DeleteEvent (calendar);
            break;
        }
        }
        while (choice !=1);
    } //End of the main function

    void menu ()
    {
        printf("___________________________________________________________________________\n");
        printf("\tHere is the menu:\n");
        printf("\t1. Quit the Program.\n");
        printf("\t2. Add an event to the calendar.\n");
        printf("\t3. Display events for a specific calendar date.\n");
        printf("\t4. Display the whole calendar.\n");
        printf("\t5. Delete the event from the calendar.\n"); 
        printf("____________________________________________________________________________\n");
        printf("\tYour choice Please: ");
    }

    void InitializeCalender(calentry_t calendar[][31]){
    int x, y;
        for(x = 0; x < 12; x ++) {
            if (x==1){
                for(y = 0; y < 28; y ++){ 
                    (calendar[x][y]).valid = TRUE;
                    (calendar[x][y]).num_events = FALSE;
                }
                for (y=28; y< 31; y++){
                    (calendar[x][y]).valid = FALSE;
                }
            }
            else if  (x==3 || x==5 || x==8 || x==10){
                for(y = 0; y < 30; y ++){ 
                    (calendar[x][y]).valid = TRUE;
                    (calendar[x][y]).num_events = FALSE;

                }
                for (y=30; y< 31; y++){
                    (calendar[x][y]).valid = FALSE;
                }
            }
            else if (x==0 || x==2||x==4||x==6||x==7||x==9||x==11){
                for(y = 0; y < 31; y ++){ 
                    (calendar[x][y]).valid = TRUE;
                    (calendar[x][y]).num_events = FALSE;
                }
            }
        }
    }

    int ReadEvent (calentry_t calendar[][31] ,char eventname[], char month[], int *day, int *year, int *h, int *min){

        char temp, answer;
        int monthnum;

            printf ("\n\tOkay, for event number %d:\n",counter+1 );
            printf ("\n\tWhat is the name of the event? ");
            scanf ("%c",&temp);
            gets (eventname);

            printf("\n\twhat is the date of the event? input like ( 12 January 2012 ) ");   
            scanf ("%d %s %d", day, month, year);
            monthnum = MonthStr2Num (month);
                if ( calendar [(monthnum-1)][(*day-1)]. valid ==FALSE || *day>=32 ){
                    while ( calendar [(monthnum-1)][(*day-1)]. valid ==FALSE || *day>=32 ) { //Loop that checks invalid dates
                    printf ("\tSorry that'san invalid date!!!!!\n");
                    printf("\n\twhat is the date of the event? input like ( 12 January 2012 ) ");   
                    scanf ("%d %s %d", day, month, year);
                    monthnum = MonthStr2Num (month);
                    } 
                }


            printf ("\n\tWhat is the time of the event? input like (12:30) ");
            scanf ("%d:%d", h,min);
                if ( *h>=24 || *h<0 || *min>=60 || *min<0){ //Loop that checks invalid times
                    while ( *h>=24 || *h<0 || *min>=60 || *min<0){
                        printf ("\tInvalid Time !!!!! \n");
                        printf ("\n\tWhat is the time of the event? input like (12:30) ");
                        scanf ("%d:%d", h,min);
                    }
                }
            monthnum = MonthStr2Num (month);
            return monthnum;

    }
    int MonthStr2Num ( char* month){
            int i,
                m=0;
        // Converting all the month characters to uppercase in order to handle all the cases of how the user 
        // enters the month
        for(i=0;i<=strlen(month);i++)
        {
        if(month[i]>=97&&month[i]<=122)
        month[i]=month[i]-32;
        }

        if (strcmp((month), "JANUARY")==0){ 

            m = 1;}

        else if (strcmp((month), "FEBRUARY")==0)
        m = 2;
        else if (strcmp((month), "MARCH")==0)
        m = 3;
        else if (strcmp((month), "APRIL")==0)
        m = 4;
        else if (strcmp((month), "MAY")==0)
        m = 5;
        else if (strcmp((month), "JUNE")==0)
        m = 6;
        else if (strcmp((month), "JULY")==0)
        m = 7;
        else if (strcmp((month), "AUGUST")==0)
        m = 8;
        else if (strcmp((month), "SEPTEMBER")==0)
        m = 9;
        else if (strcmp((month), "OCTOBER")==0)
        m = 10;
        else if (strcmp((month), "NOVEMBER")==0)
        m = 11;
        else if (strcmp((month), "DECEMBER")==0)
        m = 12;
        return m;
    }

    void AddEvent (calentry_t calendar[][31], char eventname[], int* monthnum, int *day, int *year, int *h, int *min){
        char monthstr [20];
        strcpy(monthstr,MonthNum2Str(monthnum));
        strcpy ( calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].ev_name,eventname );
        strcpy ( calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].dt.month,monthstr );  
        calendar [(*monthnum-1)][(*day-1)].num_events++;
        calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].dt.day = (*day);
        calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].dt.year = (*year); 
        calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].tm.hours = (*h);
        calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].tm.minutes = (*min);           
        counter++;

    }

    char* MonthNum2Str (int* m){ 
        if (*m==1)
        return "January";
        else if (*m==2)
        return "February";
        else if (*m==3)
        return "March";
        else if (*m==4)
        return "April";
        else if (*m==5)
        return "May";
        else if (*m==6)
        return "June";
        else if (*m==7)
        return "July";
        else if (*m==8)
        return "August";
        else if (*m==9)
        return "September";
        else if (*m==10)
        return "October";
        else if (*m==11)
        return "November";
        else if (*m==12)
        return "December";
    }

    void DisplayCalendar (calentry_t calendar[][31], int* monthnum, int* day, int* year){
        int i;

        if ( calendar [(*monthnum-1)][(*day-1)].num_events==0)
            printf ("\n\tNo events on this date!!!!!!\n");
            else{
            printf ("\n\tOn %d-%s-%d You have %d event(s): \n", *day, calendar [(*monthnum-1)][(*day-1)]. event_list[(calendar[(*monthnum-1)][(*day-1)].num_events)-1].dt.month, *year, calendar[(*monthnum-1)][(*day-1)].num_events);
                for (i=0; i<calendar[(*monthnum-1)][(*day-1)].num_events; i++){
                    printf ("\t\n Event %d: ", i+1);
                    printf ("\t\n Name of the event: %s", calendar [(*monthnum-1)][(*day-1)]. event_list[i].ev_name );
                    printf ("\t\n Time of the event: %d:%d\n", calendar [(*monthnum-1)][(*day-1)]. event_list[i+1].tm.hours, calendar [(*monthnum-1)][(*day-1)]. event_list[i+1].tm.minutes);
                }
            }
    }

    void DisplayWholeCalendar ( calentry_t calendar[][31]){
        int x, y, i;

        printf ("You have a total of %d event(s)\n", counter);

        for(x = 0; x < 12; x ++) {
            for (y=0; y< 31; y++){
                if ((calendar[x][y]).valid == FALSE ||(calendar[x][y]).num_events == FALSE){
                }
                else {
                printf ("On %d-%s-%d You have %d event(s): \n", y+1, calendar [x][y]. event_list[(calendar[x][y].num_events)-1].dt.month, calendar [x][y]. event_list[calendar[x][y].num_events].dt.year, calendar[x][y].num_events);
                    for (i=0; i<calendar[x][y].num_events; i++){
                    printf ("\t\n Event %d: ", i+1);
                    printf ("\t\n Name of the event: %s", calendar [x][y]. event_list[i].ev_name );
                    printf ("\t\n Time of the event: %d:%d\n", calendar [x][y]. event_list[i+1].tm.hours, calendar [x][y]. event_list[i+1].tm.minutes);
                    }
                }
            }
        }
    }

    int DeleteEvent (calentry_t calendar [][31]){
        int day, year, monthnum, choice, c;
        char month [20];

        printf ("\n\tWhat is the date that you want to delete one of its events? input like ( 12 January 2012 )  ");
        scanf ("%d %s %d", &day, month, &year);
        monthnum = MonthStr2Num (month);
        if ( calendar [(monthnum-1)][(day-1)].num_events==0){
            printf ("No events on this date!!!!!!\n");
            return 0;
        }
        if ((calendar[(monthnum-1)][(day-1)]).valid == FALSE){
            printf ("Invalid date !!!!\n");
            return 0;
        }
        else {
        DisplayCalendar (calendar ,&monthnum, &day, &year);
        printf ("\n\nWhat is the event that you want to delete (press 1 for event 1 or 2 for event 2...) ");
        scanf ("%d", &choice);
            if ( choice >= (calendar [(monthnum-1)][(day-1)].num_events)+1 )
                 printf("\n\tDeletion not possible.\n");

            else {
                for ( c = choice - 1 ; c < (calendar [monthnum-1][day-1].num_events) ; c++ ){
              calendar [monthnum-1][day-1]. event_list[c] =calendar [monthnum-1][day-1]. event_list[c+1];   
                }         calendar [monthnum-1][day-1].num_events--;
                            printf ("\n\tEvent deleted \n");
                            counter--;
            }
        }


    }

Can anyone help! Thank you in advance.

user1680944
  • 537
  • 2
  • 13
  • 23
  • Regarding appending text/data to a file please look at the following http://linux.die.net/man/3/fopen, particularly the `a` and `a+` modes. – nonsensickle Nov 27 '12 at 23:31
  • As for the specification, it seems pretty clear. It's asking you to print something to the file every time you invoke the `Add` or `Delete` functions and possibly when you invoke the `Read` function too. – nonsensickle Nov 27 '12 at 23:34

0 Answers0