Writing to A NON-Existing File in C

Dennis G. Rears FSAC drears at ardec.arpa
Sun Apr 10 03:22:49 AEST 1988


  ok. ok.  After over 20 messages I admit I f*cked up on my posting
and program.  The errors pointed out were:

	1) comment should have ended in "*/" not "*?"
	2) The declaration #define NULL  "/dev/null/"
	should not have used NUll and the / after null was wrong
	3) I use the word FILE instead of FILENAME in my stat(2) call
	
Next time I will not send a program while sleeping.  Please no more
messages about this.


Here is a corrected version of it:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FILENAME  "/usr/foobar"      /*  not a trademark of AT&T */
#define DEVNULL	  "dev/null"
main(a,b)
int a;
char *b[];
{
	struct stat buf;
	FILE	*fopen(), *fp;


	if(stat(FILENAME,&buf)){

		if ( (fp=fopen(DEVNULL,"a")) == NULL ) { 
			/* or use perror() */
			(void)fprintf (stderr, 
			"%s: Can not open /dev/null file\n",b[0]);
			exit(-1);
		}
	}
	else
		if ( (fp=fopen(FILENAME,"a")) == NULL ) { 
			(void)fprintf (stderr, 
			"%s: Can not open %sfile\n",b[0],FILENAME);
			exit(-1);

		}
/*   go on writing to filepointer fp */
}
------------------------------------------------------------
ARPA:		drears at ardec-ac4.arpa
UUCP:   	...!uunet!ardec-ac4.arpa!drears
AT&T:		201-724-6639
Snailmail:	Box 210, Wharton, NJ 07885
Govt Nonmail:	US Army ARDEC, ATTN SMCAR-FSS-E, Dennis Rears
		Bldg 94, Picatinny Arsenal, NJ 07806
Flames:		/dev/null
------------------------------------------------------------



More information about the Comp.unix.wizards mailing list