home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16776 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.8 KB  |  92 lines

  1. Path: sparky!uunet!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!uicvm.uic.edu!u17868
  2. Organization: University of Illinois at Chicago
  3. Date: Thursday, 19 Nov 1992 00:06:22 CST
  4. From: <U17868@uicvm.uic.edu>
  5. Message-ID: <92324.000622U17868@uicvm.uic.edu>
  6. Newsgroups: comp.lang.c
  7. Subject: fwrite & fread
  8. Lines: 82
  9.  
  10. I am using write & fread to read and write to a file. The structure that I
  11. am writing has 194 bytes. After executing fwrite I check the value, in vari
  12. able a which is 1 (correct), because I am writing one structure. Then I check
  13. the values of returned by fclose and ferror and they are also correct i-e 0.
  14. But when I quit from the program without writing more structures or without
  15. running any other functions and look for the size of my file it shows that it
  16. has 414 bytes. Actually, it should have 194 bytes. I have tried with different
  17. file modes, but that doesn't solve the problem. I have also tried with a file
  18. initially empty and with a file having some records. Therefore, I tried clearin
  19. g the buffer, but that also doesn't help. For those of you who might be interes
  20. ted in the code, I have it below. Don't pay attention towards syntax errors.
  21. I have also included a part of my header file which contains the structure.
  22. Thanks in advance,
  23. Sikander Waheed    email: u17868@uicvm.cc.uic.edu
  24.  
  25. #include <stdio.h>
  26.  
  27. #include <stdlib.h>
  28. typedef struct activity{
  29.     int  id;
  30.     int  mm;
  31.     int  dd;
  32.     int  yy;
  33.     char day[10];
  34.     int  hour;
  35.     int  min;
  36.     int  end_hour;
  37.     int  end_min;
  38.     char location[25];
  39.         char with_who[20];
  40.     char phone[11];
  41.     char subj[30];
  42.     char note[300];
  43.         int  flag;
  44. };
  45.  
  46. void add()
  47. {
  48. int a,mm,dd,yy,hour,min,st_hour, end_min = 0 , t2_hour, t2_min, i = 0, ok;
  49. char ch;
  50. FILE *file_pointer;
  51. struct activity temp;
  52.  ok = busy(mm, dd, yy, hour, t2_hour);
  53.  if(ok == 1)
  54.    {
  55.      printf("Schedule can be added\n");
  56.      file_pointer = fopen("activity.dat","ab+");
  57.      setvbuf(file_pointer, NULL, 195, _IOFBF);
  58.      temp.id = 1234;
  59.      temp.mm = mm;
  60.      temp.dd = dd;
  61.      temp.yy = yy;
  62.      temp.hour = hour;
  63.      temp.min = min;
  64.      temp.end_hour = st_hour;
  65.      temp.end_min = end_min;
  66.      temp.flag = 1;
  67.      printf("Enter the day of the activity ");
  68.      gets(temp.day);
  69.      printf("Enter the location of the activity ");
  70.      gets(temp.location);
  71.      printf("Enter the name of the person ");
  72.      gets(temp.with_who);
  73.      printf("Enter the phone number of the person ");
  74.      gets(temp.phone);
  75.      printf("Enter the subject of the activity ");
  76.      gets(temp.subj);
  77.      printf("Enter the note about the activity ");
  78.      gets(temp.note);
  79.       temp.note[i] = '\0';
  80.  
  81.      a = fwrite(&temp, sizeof(temp), 1, file_pointer);
  82.      a = ferror(file_pointer);
  83.      a = fclose(file_pointer);
  84.      a = ferror(file_pointer);
  85.      setvbuf(file_pointer, NULL , 194, _IOFBF);
  86.      ch = getchar();
  87.      } /* end if */
  88.     return;
  89. }
  90.  
  91.  
  92.