Writing to A NON-Existing File in "C"

Larry McVoy lm at arizona.edu
Wed Apr 6 17:28:12 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

------ UNTESTED! -------

If you are using FILE*

    # include	<stdio.h>
    # include	<sys/types.h>
    # include	<sys/stat.h>

    FILE* myopen(name)
    char* name;
    {
	struct stat buf;
	extern errno;

	if (stat(name, &buf) == -1  && errno == ENOENT) 
	    return fopen("/dev/null", "w");
	else
	    return fopen(name, "w");
    }

Else

    myopen(name)
    char* name;
    {
	struct stat buf;
	extern errno;

	if (stat(name, &buf) == -1  && errno == ENOENT) 
	    return open("/dev/null", 1);
	else
	    return open(name, 1);
    }

How's that?
-- 
"These aren't my thoughts, they're my cat walking on the keyboard."

Larry McVoy	lm at arizona.edu or ...!{uwvax,sun}!arizona.edu!lm



More information about the Comp.unix.wizards mailing list