Two points (Re: One more point regarding = and == (more flamage))

Blair P. Houghton bhoughto at hopi.intel.com
Fri Apr 5 10:39:34 AEST 1991


In article <15705 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <3646 at inews.intel.com> bhoughto at 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 <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>

    ...
	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).



More information about the Comp.lang.c mailing list