Click to See Complete Forum and Search --> : Creating temp files with unique names.


Sean
June 20th, 1999, 09:49 PM
In my MFC MDI, when the user opens a file the program needs to create a temporary file. So because it is MDI I need to create a different temporary file name for each file opened, but I don't know how to do this. At the moment, my temp. file is called temp.txt, but this means that if the user opens a second input file my program crashes. How can I create different filenames for these temp files? I would like to use filenames of either of following formats: <inputfile>.tmp or temp1.txt, temp2.txt .
Thank you,
Sean.

Josh Handley
June 21st, 1999, 08:58 AM
Check out the WIN32 function GetTempFileName or the ANSI C function tmpnam.

You can also roll your own by building a string out of a a fixed prefix and a number that you keep incrementing. Before you create the next temp file search the directory you are writing the files to and if there is already a file with the name you want to use then increment the number. Much simpler to use one of the above functions.

Josh