Creating a Temporary File

Creating a Temporary File

Many applications create temporary files that exist as long as the program is running and are later discarded. For example, a Web browser can store a list of pages that have been viewed during the current browsing session and redisplay them when you press “Back”. The problem is that most applications don’t delete these temporary files after the application is closed. Instead, they store them in a special directory that you have to clean manually. A better approach is to use the standard function tmpfile(), which is declared in as follows:

 FILE * tmpfile();

tmpfile() opens a temporary file for read/write operations and returns a pointer to to that file. tmpfile() uses unique filenames to avoid conflicts with any existing files. The temporary files created by tmpname() are automatically deleted when the program terminates so you don’t have to manually delete them.


Share the Post: