home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!cs.utexas.edu!torn!csd.unb.ca!elara.sun.csd.unb.ca!d0np
- From: d0np@elara.sun.csd.unb.ca (Godzilla)
- Subject: Re: fwrite & fread
- Message-ID: <1992Nov19.131529.1043@jupiter.sun.csd.unb.ca>
- Sender: news@jupiter.sun.csd.unb.ca
- Organization: University of New Brunswick
- References: <92324.000622U17868@uicvm.uic.edu>
- Date: Thu, 19 Nov 1992 13:15:29 GMT
- Lines: 75
-
- #In article <92324.000622U17868@uicvm.uic.edu>, <U17868@uicvm.uic.edu> writes:
- |> I am using write & fread to read and write to a file. The structure that I
- |> am writing has 194 bytes. After executing fwrite I check the value, in vari
- |> able a which is 1 (correct), because I am writing one structure. Then I check
- |> the values of returned by fclose and ferror and they are also correct i-e 0.
- |> But when I quit from the program without writing more structures or without
- |> running any other functions and look for the size of my file it shows that it
- |> has 414 bytes. Actually, it should have 194 bytes. I have tried with different
- |> file modes, but that doesn't solve the problem. I have also tried with a file
- |> initially empty and with a file having some records. Therefore, I tried clearin
- |> g the buffer, but that also doesn't help. For those of you who might be interes
- |> ted in the code, I have it below. Don't pay attention towards syntax errors.
- |> I have also included a part of my header file which contains the structure.
- |> Thanks in advance,
- |> Sikander Waheed email: u17868@uicvm.cc.uic.edu
- |>
- |> #include <stdio.h>
- |>
- |> #include <stdlib.h>
- |> typedef struct activity{
- |> int id;
- |> int mm;
- |> int dd;
- |> int yy;
- |> char day[10];
- |> int hour;
- |> int min;
- |> int end_hour;
- |> int end_min;
- |> char location[25];
- |> char with_who[20];
- |> char phone[11];
- |> char subj[30];
- |> char note[300];
- |> int flag;
- |> };
- |>
- |> void add()
- |> {
- |> int a,mm,dd,yy,hour,min,st_hour, end_min = 0 , t2_hour, t2_min, i = 0, ok;
- |> char ch;
- |> FILE *file_pointer;
- |> struct activity temp;
- |> ok = busy(mm, dd, yy, hour, t2_hour);
- |> if(ok == 1)
- |> {
- |>
- .
- . (stuff deleted)
- .
-
- |> a = fwrite(&temp, sizeof(temp), 1, file_pointer);
-
- You are writing out sizeof(temp) number of bytes, and since temp is a
- struct activity, which has 300+30+11+20+25+10+9*2 = 414 bytes assuming
- int is 2 bytes. Try printing sizeof(temp) to see what you get.
-
- |> a = ferror(file_pointer);
- |> a = fclose(file_pointer);
- |> a = ferror(file_pointer);
- |> setvbuf(file_pointer, NULL , 194, _IOFBF);
- |> ch = getchar();
- |> } /* end if */
- |> return;
- |> }
- |>
- |>
-
-
- Hope this helps
- Mike
-
-
- d0np@jupiter.sun.csd.unb.ca
- d0np@unb.ca
-