65.9K
CodeProject is changing. Read more.
Home

Generate temporary files with any extension

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (2 votes)

Jan 12, 2012

CPOL
viewsIcon

28610

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:

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;
}