将日历写入文本文件而不覆盖以前的数据C.

时间:2012-11-27 22:58:51

标签: c text-files binaryfiles logfiles

我被要求使用结构和文件来实现日历。我成功地使用结构实现它。但是,为了处理文件,我必须在我的程序之上构建。看起来具有挑战性的主要想法是如何在不覆盖以前数据的情况下写入文本和二进制文件。以下是我给出的一些规范:

  1. 将日历保存为文本文件
  2. 将日历保存为二进制文件
  3. 从文字中阅读日历
  4. 将日历保存为二进制文件
  5. 您的程序的另一个附加功能是记录您自上次阅读以来对日历所做的更改的文本文件。在这里您应该使用允许您重新加载原始日历并重新加载记录的文件格式如果你的程序崩溃所以你会失去很少的变化来重新获得日历的状态。您应该以两种方式实现这一点:

    1. 作为显式菜单选项,将所有更改记录到名为" eventlog_time.txt"的文件中,其中时间是从" time.h"库。
    2. 作为自动记录操作。该程序保存一个名为" eventlog_private.txt"每次用户添加或删除事件时它都会自动更新。您需要将此文件初始化为空,然后将其打开,附加到该文件,并在记录每个事件后将其关闭。
    3. 这是我写的代码:

      任何人都可以向我解释规范的最后一部分,因为我不确定我应该做什么,或者至少给我一个暗示以启动问题。 这是我的代码:

              /************************************************************************************************
          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--;
                  }
              }
      
      
          }
      

      任何人都可以帮忙! 提前谢谢。

0 个答案:

没有答案