perror, yet again

Andrew Walduck andrew at motto.UUCP
Wed Dec 5 07:16:54 AEST 1990


Peter da Siva writes:
>(where the error might not have had anything to do with stdio). In general
>the simple code:
>
>	if(!(fp = fopen(...))) {
>		perror(...);
>		...
>	}
>does a perfectly adequate job of printing a usable error message. It would
>be nicer to have ferror(fp) return an error number suitable for feeding
>to strerror(), but in the meantime it's better than nothing.
>-- 
>Peter da Silva.   `-_-'
>+1 713 274 5180.   'U`
>peter at ferranti.com 

Actually, a more correct (ANSI) implementation would be:

	if((fp = fopen(...)) == NULL) {
		perror(...);
		...
	}

as fopen returns NULL upon failure...and NULL is NOT necessarily zero,
which is assumed in your code above.  

Andrew
 ------------------------------------------------------
| Andrew Walduck | andrew at motto.UUCP | Motorola Canada |
 ------------------------------------------------------



More information about the Comp.lang.c mailing list