Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!batcomputer!cornell!uw-beaver!milton!ogicse!intelhf!ichips!iwarp.intel.com!inews!hopi!bhoughto From: bhoughto@hopi.intel.com (Blair P. Houghton) Newsgroups: comp.lang.c Subject: Two points (Re: One more point regarding = and == (more flamage)) Message-ID: <3661@inews.intel.com> Date: 5 Apr 91 00:39:34 GMT Article-I.D.: inews.3661 References: <29444:Mar3120:23:3491@kramden.acf.nyu.edu> <3646@inews.intel.com> <15705@smoke.brl.mil> Sender: news@inews.intel.com Organization: Intel Corp, Chandler, AZ Lines: 44 In article <15705@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: >In article <3646@inews.intel.com> bhoughto@nevin.intel.com (Blair P. Houghton) writes: >> while ( ( c = (char) getchar() ) != (char)EOF ) > >Assuming that the loop is meant to handle all possible byte values, >this is also buggy. I generally use read(2) or fread(3) when I expect binary input, but no, it has no trouble with ascii(7). --Blair "But, Dougiedougiedougiedougie did the _other_ one work, huh, diditdiditdiditdidit huhhhh?" P.S. In case you're tired of the bickering, here's a value-added oldie-but-a-goodie: the file-slurp (with tons of error-checking and the occasional type-cast omitted, some data inefficiencies, and maybe even a typo or two... :-). #include #include #include ... struct stat stb; char *buf; FILE *fp; stat( "filename", &stb ); buf = (char *) malloc ( stb.st_size * (sizeof (char *)) ); fp = fopen( "filename", "r"); fread( buf, stb.st_size, 1, fp ); /* yippee! */ fclose(fp); ... The moral of this story is: let fread worry about the blocks and bytes (although you may want to explore stat(2) to figure out how to deal with pipes, links, etc., when you can't guarantee an existing file on the disk).