Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Generate temporary files with any extension

4.50/5 (2 votes)
12 Jan 2012CPOL 28.6K  
How to get temporary files with any extension.

There isn't any Win32 API to generate temporary files with any extension, so I wrote my own function:


C++
BOOL GetTemporaryFilePath(CString strExtension, CString& strFileName)
{
     TCHAR lpszTempPath[MAX_PATH] = { 0 };
     if (!GetTempPath(MAX_PATH, lpszTempPath))
         return FALSE;

     TCHAR lpszFilePath[MAX_PATH] = { 0 };
     do {
         if (!GetTempFileName(lpszTempPath, NULL, 0, lpszFilePath))
             return FALSE;

         strFileName = lpszFilePath;
         VERIFY(::DeleteFile(strFileName));
         strFileName.Replace(_T(".tmp"), strExtension);
     }
     while (_taccess(strFileName, 00) != -1);

     OutputDebugString(_T("GetTemporaryFilePath = '") + strFileName + _T("'\n"));
     return TRUE;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)