Windows編程 之臨時檔案 (筆記)

來源:互聯網
上載者:User

1.  函數說明

1.1  GetTempFileName

    建立一個臨時檔案,並返回臨時檔案的名字

【1】 lpPathName 路徑名

【2】lpPrefixString 首碼

【3】uUnique 是否唯一,0為唯一,否則不是。當設定為0時,會建立一個空檔案,否則只是返回一個檔案名稱

【4】 返回的檔案名稱,格式為  <path>\ <pre><uuuu>.TMP 

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

2. 代碼

/* * 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;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.