mike@artsvax.UUCP (12/01/87)
I'm trying to read entire large files into memory on the Mac. (for speed in writing them out) Since I can't figure out how get the total size of a file, I devised the following code. Unfortunately, it doesn't seem to work correctly. Any comments would be appreciated. For some reason, it always reads 79 blocks from every file when run from a MacII, and 44 blocks when run from a Plus. #define MAXBUFF 153600 /* room for three hundred blocks */ do_read_file(fp) FILE *fp; { unsigned char buffer[300 * 512]; unsigned char *buff; /* I would have used malloc or calloc, but */ /* everytime I called them the system crashes */ buff = buffer; while((!feof(fp) && ((buff - buffer) < MAXBUFF)){ length = fread(buff,sizeof(char),512,fp); buff += length; } fclose(fp); } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Michael S. Czeiszperger | Disclaimer: "Sorry, I'm all out of pith" Systems Programmer I | Smail: Room 406 Baker (614) College of the Arts | 1971 Neil Avenue 292- Computer Lab | Columbus, OH 43210 0895 The Ohio State University | UUCP: {decvax,ucbvax}!cbosgd!osupyr!artsvax!mike ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jwhitnel@csi.UUCP (12/02/87)
In article <213@artsvax.UUCP> mike@artsvax.UUCP (Michael Czeiszperger) writes: >#define MAXBUFF 153600 /* room for three hundred blocks */ >do_read_file(fp) >FILE *fp; >{ > > unsigned char buffer[300 * 512]; > unsigned char *buff; /* I would have used malloc or calloc, but */ > /* everytime I called them the system crashes */ Use mlalloc (sp)? for larger then 32K > > buff = buffer; > while((!feof(fp) && ((buff - buffer) < MAXBUFF)){ > length = fread(buff,sizeof(char),512,fp); > buff += length; > } Why not use the much simpler: long length; char * buffer; unsigned char *buffer; buffer = mlalloc(MAXBUFF); if ( buffer == NULL ) /* Not enough memory */ length = fread( buffer, 512, 300, fp); return( length ); > Michael S. Czeiszperger | Disclaimer: "Sorry, I'm all out of pith" Jerry Whitnell Lizzi Borden took an axe Communication Solutions, Inc. And plunged it deep into the VAX; Don't you envy people who Do all the things You want to do?