open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com
Creating a Temporary File
LINUX TIPS AND TRICKS --- 08/16/2002

Danny Kalev

Many applications create temporary files that should be deleted after the process terminates. For example, a Web browser caches pages that have been viewed during the current browsing session in temporary files, while some applications use per-session log files. The snag is that most programs don't delete these temporary files when they terminate. Instead, they store them in a special directory that you have to expunge manually every once in awhile. Most programmers aren't aware of two functions that automate most of this drudgery. 

On this topic

Generating Unique Filenames
If you only need to generate unique filenames, then use the standard function tmpnam(). tmpnam() is declared in <stdio.h> as follows:

char * tmpnam(char * name);

The argument 'name' contains a pointer to a buffer that has at least L_tmpnam characters. If you pass a NULL pointer as an argument, then tmpnam() stores the generated filename in an internal static buffer and returns its address. Remember to copy the returned filename to another buffer before calling tmpnam() once more, as each call overwrites the previous filename. tmpnam() returns a filename that doesn't conflict with any other file in the current directory. You can call tmpnam() up to TMP_MAX (the TMP_MAX and L_tmpnam constants are defined in <stdlib.h>) times without generating repeated names. The following program calls tmpnam() 10 times and prints the generated names:

#include <stdio.h>
int main() { char name[L_tmpnam]; for (int n=0; n<10; ++n) { tmpnam(name); printf("%s", name); } }

Generating Temporary Files
tmpnam() only generates unique filenames; it doesn't free you from manually opening those files and deleting them afterwards. The tmpfile() function, on the other hand, does all of that for you. The tmpfile() function is declared in <stdio.h> as follows:

FILE * tmpfile();

This function opens a temporary file for read/write operations and returns a FILE* associated with that file. tmpfile()avoids conflicts with any existing files by using tmpnam() internally. Files created by tmpfile() are automatically deleted when the program terminates, thus you don't have to manually delete them.

 

Danny Kalev is a system analyst and software engineer with more than 10 years of experience, specializing in C++ and object-oriented analysis and design on various platforms including VMS, DOS, Windows, Unix, and Linux. His technical interests involve code optimization, networking, and distributed computing. He is also a member of the ANSI C++ standardization committee and the author of ANSI/ISO C++ Professional Programmer's Handbook (Que, 1999). Danny can be reached at Danny.Kalev@itworld.com.



Advertisements
Sponsored links
Bring harmony to your mix of UNIX-Linux-Windows computing environments
Top 5 Reasons to Combine App Performance and Security
Locate Hidden Software on business PCs with this free tool
KODAK i1400 Series Scanners stand up to the challenge
 Home   Newsletters  LINUX TIPS AND TRICKS
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.