Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!uokmax!servalan!epmooch!ben From: ben@epmooch.UUCP (Rev. Ben A. Mesander) Newsgroups: comp.sys.amiga.programmer Subject: Re: SAS C Bug?? Standard FILE command does not work!! Message-ID: Date: 27 Jan 91 01:25:45 GMT References: <1991Jan27.221835.1230@rodan.acs.syr.edu> Lines: 63 >In article <1991Jan27.221835.1230@rodan.acs.syr.edu> goldberg@rodan.acs.syr.edu (Ross Goldberg) writes: >I am trying to get a procedure working to read and write records to the >disk. This is accomplished in many books by using fwrite and fread. >The sample below comes from one of the books...the problem is: [...] >#include >main () >{ > typedef struct { > char name[15]; > int midterm; > } grades; >int i,number; >char name[50]; >grades student; >FILE *fp; >printf("class file "); >scanf("%s",name); >if (NULL!=(fp=fopen(name,"w"))) { > printf("number of students"); > scanf("%d",&number); > for (i=0;i printf("student's name: "); > scanf("%[^\n]",student.name); > printf("midterm score: "); > scanf("%d",&(student.midterm)); > fwrite (&student,sizeof(student),1,fp); ^^^^^^ ^^^^^^^^ > } [...] >There is the book sample. >As I said, where you see the fwrite (&student,sizeof(student),1,fp); >it gives the warning for the &student >Ross Goldberg From the Fine Manual for SAS/C: SYNOPSIS #include a = fwrite(b,size,n,fp); int a; actual number of blocks char *b; pointer to first block ^^^^^^^^^^ int bsize; size of block in bytes int n; maximum number of blocks FILE *fp; file pointer So, the first argument to fwrite needs to be a pointer to a character. You need to cast the pointer to your structure to a pointer to a character. This is consistent with the definition of the C language, and is evidently a bug in the program you typed in. Was the book pre-ANSI? I don't know, my ancient UNIX reference manual lists the first argument to fwrite() as a pointer to a character, so it's been that way for a very long time. Those books aren't perfect, you know! -- | ben@epmooch.UUCP (Ben Mesander) | "Cash is more important than | | ben%servalan.UUCP@uokmax.ecn.uoknor.edu | your mother." - Al Shugart, | | !chinet!uokmax!servalan!epmooch!ben | CEO, Seagate Technologies |