Writing to A NON-Existing File in \"C\"

Larry DeLuca henrik at blblbl.UUCP
Sun Apr 10 08:13:21 AEST 1988


In article <12840 at brl-adm.ARPA>, mchinni at ardec.arpa (Michael J. Chinni, SMCAR-CCS-E) writes:
> In a message from Michael Deutsch  <deutsch at jplgodo.uucp> 
> dated 6 Apr 88 03:47:17 GMT he writes:
>  > exists.  In case the file DOES NOT exist I want
>  > the program to function identically but the results
>  > should be flushed down the tube, i.e. nowhere, i.e.

> I have a couple of suggestions.  First, try writing to "/dev/null".
> Second, a temporary file could be used ("mkstemp" in BSD or "tmpnam"/"tempnam"
> and "tmpfile" in SysV).  Last, a filename of personal choice could be created,
> written to, and then deleted at the end of the program.

Why not just open the file at the beginning, and use the file pointer returned
to tell if you should write or not, i.e.

	if(fp != (FILE *) 0)
		fprintf(...);

I usually encode things of this sort in a macro and it makes my life much 
easier.  Also, if you don't have the file around in the first place, things
will go much more quickly than writing to a temp file you're only going to 
delete, or writing to /dev/null.

					larry...



More information about the Comp.unix.wizards mailing list