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

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