Writing to A NON-Existing File in "C"

William E. Davidsen Jr davidsen at steinmetz.steinmetz.ge.com
Thu Apr 7 00:23:31 AEST 1988


In article <9654 at jplgodo.UUCP> deutsch at jplgodo.UUCP (Michael Deutsch ) writes:
| 
| I have a "C" program that records the program
| results into a file, provided that file already
| 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.
| written to a non-existing file?

  First problem is to open the file if it exists, by
	fp = fopen(MYFILE, "r+");	/* open, no create	*/
	outflag = fp != NULL;		/* set a flag		*/
	if (outflag) fseek(fp, 0L, 2);	/* rewind if open ok	*/

  You can then print to the file using a macro, such as:
	#define cprintf if (outflag) fprintf
	.
	.
	cprintf(fp, "format", data);

-- 
	bill davidsen		(wedu at ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.unix.questions mailing list