home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!uicvm.uic.edu!u17868
- Organization: University of Illinois at Chicago
- Date: Thursday, 19 Nov 1992 00:06:22 CST
- From: <U17868@uicvm.uic.edu>
- Message-ID: <92324.000622U17868@uicvm.uic.edu>
- Newsgroups: comp.lang.c
- Subject: fwrite & fread
- Lines: 82
-
- 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)
- {
- printf("Schedule can be added\n");
- file_pointer = fopen("activity.dat","ab+");
- setvbuf(file_pointer, NULL, 195, _IOFBF);
- temp.id = 1234;
- temp.mm = mm;
- temp.dd = dd;
- temp.yy = yy;
- temp.hour = hour;
- temp.min = min;
- temp.end_hour = st_hour;
- temp.end_min = end_min;
- temp.flag = 1;
- printf("Enter the day of the activity ");
- gets(temp.day);
- printf("Enter the location of the activity ");
- gets(temp.location);
- printf("Enter the name of the person ");
- gets(temp.with_who);
- printf("Enter the phone number of the person ");
- gets(temp.phone);
- printf("Enter the subject of the activity ");
- gets(temp.subj);
- printf("Enter the note about the activity ");
- gets(temp.note);
- temp.note[i] = '\0';
-
- a = fwrite(&temp, sizeof(temp), 1, file_pointer);
- a = ferror(file_pointer);
- a = fclose(file_pointer);
- a = ferror(file_pointer);
- setvbuf(file_pointer, NULL , 194, _IOFBF);
- ch = getchar();
- } /* end if */
- return;
- }
-
-
-