Writing to A NON-Existing File in "C"

Alan J Rosenthal flaps at dgp.toronto.edu
Sat Apr 9 04:38:03 AEST 1988


davidsen at crdos1.UUCP (bill davidsen) writes:
>	if (outflag) fseek(fp, 0L, 2);	/* rewind if open ok	*/

That's "fseek(fp,0L,0)".  The given fseek() call seeks to the end of the file.

>	#define cprintf if (outflag) fprintf

This kind of macro is very unsafe.  Consider:

	if(something)
	    cprintf(blah blah);
	else
	    something else;

The `else' clause will pair with the if in the cprintf expansion, much
to your surprise and irritation.  When at all possible, define macros
to be expressions rather than larger amounts of code.  For example:

	extern int fprintf(),mydonothing();
	#define cprintf (outflag ? fprintf : mydonothing)

and, elsewhere:

	int mydonothing()
	{
	    return(0);
	}

Some adjustments are needed to make it valid ANSI C due to mydonothing()'s
being variadic; I think fprintf() will be declared in stdio.h but I'm not sure.

ajr
-- 
"Comment, Spock?"
"Very bad poetry, Captain."




More information about the Comp.unix.wizards mailing list