VC获取系统临时文件夹temp
生活随笔
收集整理的這篇文章主要介紹了
VC获取系统临时文件夹temp
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MSDN:創建和使用一個臨時文件
一、介紹
系統臨時文件夾可用%tmp%或者%temp%查看,路徑為C:\Users\pc\AppData\Local\Temp獲取臨時文件夾路徑GetTempPath, GetTempFileName獲取臨時文件夾下文件名
二、案例
這個應用程序打開一個文件指定的用戶,并使用一個臨時文件將文件轉換為大寫字母。注意,給定的源文件被認為是一個ASCII文本文件和創建的新文件覆蓋每次運行應用程序。 // ConsoleApplication1.cpp : 定義控制臺應用程序的入口點。 //#include "stdafx.h"#include "Windows.h" /* int main() {HANDLE hFile = CreateFile(TEXT("text.txt"), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile == INVALID_HANDLE_VALUE){//MessageBox(TEXT("創建文件失敗!"));return 1;}LARGE_INTEGER liDistanceToMove;liDistanceToMove.QuadPart = 102400000; //設置成這個大,單位字節if (!SetFilePointerEx(hFile, liDistanceToMove, NULL, FILE_BEGIN)){// MessageBox(TEXT("移動文件指針失敗!"));}if (!SetEndOfFile(hFile)){// MessageBox(TEXT("設置文件尾失敗!"));}CloseHandle(hFile);return 0; } */ #include <windows.h> #include <tchar.h> #include <stdio.h>#define BUFSIZE 1024void PrintError(LPCTSTR errDesc);int _tmain(int argc, TCHAR *argv[]) {HANDLE hFile = INVALID_HANDLE_VALUE;HANDLE hTempFile = INVALID_HANDLE_VALUE;BOOL fSuccess = FALSE;DWORD dwRetVal = 0;UINT uRetVal = 0;DWORD dwBytesRead = 0;DWORD dwBytesWritten = 0;TCHAR szTempFileName[MAX_PATH];TCHAR lpTempPathBuffer[MAX_PATH];char chBuffer[BUFSIZE];LPCTSTR errMsg;if (argc != 2){_tprintf(TEXT("Usage: %s <file>\n"), argv[0]);return -1;}// Opens the existing file. hFile = CreateFile(argv[1], // file name GENERIC_READ, // open for reading 0, // do not share NULL, // default security OPEN_EXISTING, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no template if (hFile == INVALID_HANDLE_VALUE){PrintError(TEXT("First CreateFile failed"));return (1);}// Gets the temp path env string (no guarantee it's a valid path).dwRetVal = GetTempPath(MAX_PATH, // length of the bufferlpTempPathBuffer); // buffer for path if (dwRetVal > MAX_PATH || (dwRetVal == 0)){PrintError(TEXT("GetTempPath failed"));if (!CloseHandle(hFile)){PrintError(TEXT("CloseHandle(hFile) failed"));return (7);}return (2);}_tprintf(TEXT(" %s\n"), lpTempPathBuffer);// Generates a temporary file name. uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp filesTEXT("DEMO"), // temp file name prefix 0, // create unique name szTempFileName); // buffer for name _tprintf(TEXT(" %s\n"), szTempFileName);if (uRetVal == 0){PrintError(TEXT("GetTempFileName failed"));if (!CloseHandle(hFile)){PrintError(TEXT("CloseHandle(hFile) failed"));return (7);}return (3);}// Creates the new file to write to for the upper-case version.hTempFile = CreateFile((LPTSTR)szTempFileName, // file name GENERIC_WRITE, // open for write 0, // do not share NULL, // default security CREATE_ALWAYS, // overwrite existingFILE_ATTRIBUTE_NORMAL,// normal file NULL); // no template if (hTempFile == INVALID_HANDLE_VALUE){PrintError(TEXT("Second CreateFile failed"));if (!CloseHandle(hFile)){PrintError(TEXT("CloseHandle(hFile) failed"));return (7);}return (4);}// Reads BUFSIZE blocks to the buffer and converts all characters in // the buffer to upper case, then writes the buffer to the temporary // file. do{if (ReadFile(hFile, chBuffer, BUFSIZE, &dwBytesRead, NULL)){// Replaces lower case letters with upper case// in place (using the same buffer). The return// value is the number of replacements performed,// which we aren't interested in for this demo.CharUpperBuffA(chBuffer, dwBytesRead);fSuccess = WriteFile(hTempFile,chBuffer,dwBytesRead,&dwBytesWritten,NULL);if (!fSuccess){PrintError(TEXT("WriteFile failed"));return (5);}}else{PrintError(TEXT("ReadFile failed"));return (6);}// Continues until the whole file is processed.} while (dwBytesRead == BUFSIZE);// The handles to the files are no longer needed, so// they are closed prior to moving the new file.if (!CloseHandle(hFile)){PrintError(TEXT("CloseHandle(hFile) failed"));return (7);}if (!CloseHandle(hTempFile)){PrintError(TEXT("CloseHandle(hTempFile) failed"));return (8);}// Moves the temporary file to the new text file, allowing for differnt// drive letters or volume names.fSuccess = MoveFileEx(szTempFileName,TEXT("AllCaps.txt"),MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);if (!fSuccess){PrintError(TEXT("MoveFileEx failed"));return (9);}else_tprintf(TEXT("All-caps version of %s written to AllCaps.txt\n"), argv[1]);return (0); }// ErrorMessage support function. // Retrieves the system error message for the GetLastError() code. // Note: caller must use LocalFree() on the returned LPCTSTR buffer. LPCTSTR ErrorMessage(DWORD error) {LPVOID lpMsgBuf;FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM| FORMAT_MESSAGE_IGNORE_INSERTS,NULL,error,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL);return((LPCTSTR)lpMsgBuf); }// PrintError support function. // Simple wrapper function for error output. void PrintError(LPCTSTR errDesc) {LPCTSTR errMsg = ErrorMessage(GetLastError());_tprintf(TEXT("\n** ERROR ** %s: %s\n"), errDesc, errMsg);LocalFree((LPVOID)errMsg); }總結
以上是生活随笔為你收集整理的VC获取系统临时文件夹temp的全部內容,希望文章能夠幫你解決所遇到的問題。