Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=====================_893121324==_" To: "John Doe" , djgpp AT delorie DOT com From: Nate Eldredge Subject: Re: One needs help with fwrite and fread... Cc: rufus9999 AT yahoo DOT com Date: Mon, 20 Apr 1998 18:12:21 -0700 Message-ID: <19980421011110.AAJ9311@ppp124.cartsys.com> Precedence: bulk --=====================_893121324==_ Content-Type: text/plain; charset="us-ascii" At 02:26 4/20/1998 GMT, John Doe wrote: >I wrote 2 very simple programs using fread and fwrite to write structers to >a file, when I run the 1st one, it is suppost to write the file, and the >2nd one is to read it back... well this sounds very simple, but doesn't >work. I am having a very very hard time with this, would anyone care to >write me 2 sample programs. The One of the sample programs has to write the >file of record, and the second one has to read it. This is actually off-topic for comp.os.msdos.djgpp (comp.lang.c would be better), but here you go anyway. Note: Writing structures to a file is likely to work ONLY if the file need not be used by programs compiled by any other compiler, or on any other system. Also, note the "b" option to fopen, it is very important! >thanx.. PLEASE SEND ALL EMAIL TO rufus9999 AT yahoo DOT com Maybe you should put that address in your mail header? It would make things much easier for everyone. --=====================_893121324==_ Content-Type: text/plain; charset="us-ascii" /* write.c -- Writes the file */ #include #include struct ordered_pair { double x; double y; }; /* Note: This must be the same in both files */ int main(void) { FILE *f; struct ordered_pair op; f = fopen("thefile", "wb"); /* note: the "b" is important!! */ if (!f) abort(); printf("Enter ordered pairs (x,y). Ctrl-Z RET to stop.\n"); fflush(stdout); while (scanf("%lf,%lf", &op.x, &op.y) != EOF) /* Get numbers */ fwrite(&op, sizeof(struct ordered_pair), 1, f); /* Write struct */ /* All done */ fclose(f); return 0; } /* End of write.c */ --=====================_893121324==_ Content-Type: text/plain; charset="us-ascii" /* read.c -- Reads the file */ #include #include struct ordered_pair { double x; double y; }; /* NOTE: This must be exactly the same in both files! */ int main(void) { FILE *f; struct ordered_pair op; f = fopen("thefile", "rb"); /* note: the "b" is important!! */ if (!f) abort(); printf("Ordered pairs follow.\n"); while (fread(&op, sizeof(struct ordered_pair), 1, f) > 0) /* Read numbers */ printf("%g, %g\n", op.x, op.y); /* Print them */ fclose(f); return 0; } /* End of read.c */ --=====================_893121324==_ Content-Type: text/plain; charset="us-ascii" Nate Eldredge nate AT cartsys DOT com --=====================_893121324==_--