Programming in C: Unit V: File processing

Creating a Temporary File

Programming in C | File processing

The tmpfile() function is used to create a temporary file. The tmpfile () opens the corresponding stream with access parameters set as "w+". The file created with tmpfile () will be automatically deleted when all references to the file are closed.

CREATING A TEMPORARY FILE

The tmpfile() function is used to create a temporary file. The tmpfile () opens the corresponding stream with access parameters set as "w+". The file created with tmpfile () will be automatically deleted when all references to the file are closed, i.e., the file created will be automatically closed and erased when the program has been completely executed. The prototype of tmpfile() as given is stdio.h header file is

FILE * tmpfile (void);

On success, tmpfile () will return a pointer to the stream of the file that is created. In case of an error, the function will return a null pointer and set errno to indicate the error.

The function can be used in the following manner:

FILE *tp = tmpfile();

Now the file created can be used in the same way as any other text or binary file.

Programming in C: Unit V: File processing : Tag: : Programming in C | File processing - Creating a Temporary File