Temporary files for Windows Programming (notes)

Source: Internet
Author: User

1. Function Description

1.1 GetTempFileName

Create a temporary file and return the name of the temporary file.

[1] lpPathName pathname

[2] lpPrefixString prefix

[3] whether uUnique is unique; 0 is unique; otherwise, no. When it is set to 0, an empty file is created. Otherwise, only a file name is returned.

[4] returned file name, in the format<Path>\<Pre> <uuuu>. TMP

UINT WINAPI GetTempFileName(  _In_   LPCTSTR lpPathName,  _In_   LPCTSTR lpPrefixString,  _In_   UINT uUnique,  _Out_  LPTSTR lpTempFileName);

2. Code

/* * FileName: SystemTempFile.cpp  * Author:   JarvisChu * Date:     2012-11-14 */#include <stdio.h>#include <windows.h>#include <tchar.h>   //switchs for the program//notice: do not open more than one //#define TEMP_FILE//#define PATH //#define FILE_TIME#define FILE_ATTRIBUTE#define BUFF_SIZE 256int main(){//--------------------------------------------------------#ifdef TEMP_FILEprintf("--------Temp File----------------\n");DWORD nLen;TCHAR szFileName[BUFF_SIZE];TCHAR szLongPathBuff[BUFF_SIZE];TCHAR szShortPathBuff[BUFF_SIZE];GetTempFileName(_T("E:/Programming"),    //the directory to create the temp file. '.' represents the current directory_T("jc"),   //the prefix string of the temp file0,szFileName   );//the szFileName can be string like "./jc258.tmp" or "./jc454.tmp"//and the file named "jc258.tmp" or "jc454.tmp" have been created in the current directoryprintf(_T("TempFile Name: %s\n"),szFileName); GetFullPathName(szFileName,BUFF_SIZE,szLongPathBuff,NULL);printf("FullPathName: %s\n",szLongPathBuff);  // E:/Programming\jc5EC.tmpnLen = lstrlen(szLongPathBuff);GetShortPathName(szLongPathBuff,szShortPathBuff,nLen);printf("ShortPathName: %s\n",szShortPathBuff);// E:\PROGRA~2\jc5EC.tmp#endif//--------------------------------------------------------#ifdef FILE_TIMEprintf("--------File Time----------------\n");//GetFileTime()HANDLE hFile;FILETIME ftCreate,ftAccess,ftWrite,ftLocal;SYSTEMTIME stCreate;hFile = CreateFile(_T("C:/jarvischu.txt"),GENERIC_READ|GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);GetFileTime(hFile,&ftCreate,&ftAccess,&ftWrite);// Convert the last-write time to local time.    FileTimeToLocalFileTime(&ftWrite, &ftLocal);    // Convert the local file time from UTC to system time.    FileTimeToSystemTime(&ftLocal, &stCreate);HANDLE hConsoleOut = CreateFile("CONOUT$",GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);TCHAR szBuf[BUFF_SIZE];DWORD nLen,nWrite;    // Build a string showing the date and time.    wsprintf(szBuf,"%02d/%02d/%d  %02d:%02d\n",        stCreate.wDay, stCreate.wMonth,stCreate.wYear,        stCreate.wHour, stCreate.wMinute);nLen = lstrlen(szBuf);WriteConsole(hConsoleOut,szBuf,nLen,&nWrite,NULL);#endif//--------------------------------------------------------#ifdef PATHprintf("--------FullPath & ShortPath----------------\n");DWORD nLen;TCHAR szLongPathBuff[BUFF_SIZE];TCHAR szShortPathBuff[BUFF_SIZE];GetFullPathName(_T("E:/Programming/jarvischu.txt"),BUFF_SIZE,szLongPathBuff,NULL);printf("FullPathName: %s\n",szLongPathBuff);  //E:\Programming\jarvischu.txtnLen = lstrlen(szLongPathBuff);GetShortPathName(szLongPathBuff,szShortPathBuff,nLen);printf("ShortPathName: %s\n",szShortPathBuff);//E:\PROGRA~2\JARVIS~1.TXT#endif//--------------------------------------------------------#ifdef FILE_ATTRIBUTEprintf("--------File Attribute----------------\n");DWORD dwAttr;dwAttr = GetFileAttributes("E:/Programming/jarvischu.rar");if(dwAttr != -1) // -1 stands for file_not_exists or no rights to access the file{if( dwAttr & FILE_ATTRIBUTE_ARCHIVE){printf("File is Archived!\n");}if (dwAttr & FILE_ATTRIBUTE_NORMAL){printf("File is Normal!\n");}}#endifreturn 0;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.