Creating and Using a Temporary File
来源:互联网 发布:土耳其软糖淘宝 编辑:程序博客网 时间:2024/05/18 20:07
Creating and Using a Temporary File
Applications can obtain unique file and path names for temporary files by using theGetTempFileName and GetTempPath functions. The GetTempFileName function generates a unique file name, and theGetTempPath function retrieves the path to a directory where temporary files should be created.
The following procedure describes how an application creates a temporary file for data manipulation purposes.
To create and use a temporary file
- The application opens the user-provided source text file by using CreateFile.
- The application retrieves a temporary file path and file name by using the GetTempPath and GetTempFileName functions, and then uses CreateFile to create the temporary file.
- The application reads blocks of text data into a buffer, converts the buffer contents to uppercase using theCharUpperBuffA function, and writes the converted buffer to the temporary file.
- When all of the source file is written to the temporary file, the application closes both files, and renames the temporary file to "allcaps.txt" by using theMoveFileEx function.
Each of the previous steps is checked for success before moving to the next step, and a failure description is displayed if an error occurs. The application will terminate immediately after displaying the error message.
Note that text file manipulation was chosen for ease of demonstration only and can be replaced with any desired data manipulation procedure required. The data file can be of any data type, not only text.
The GetTempPath function retrieves a fully qualified path string from an environment variable but does not check in advance for the existence of the path or adequate access rights to that path, which is the responsibility of the application developer. For more information, see GetTempPath. In the following example, an error is regarded as a terminal condition and the application exits after sending a descriptive message to standard output. However, many other options exist, such as prompting the user for a temporary directory or simply attempting to use the current directory.
The following C++ example shows how to create a temporary file for data manipulation purposes.
//// This application opens a file specified by the user and uses// a temporary file to convert the file to upper case letters.// Note that the given source file is assumed to be an ASCII text file// and the new file created is overwritten each time the application is// run.//#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 buffer lpTempPathBuffer); // 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); } // Generates a temporary file name. uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp files TEXT("DEMO"), // temp file name prefix 0, // create unique name szTempFileName); // buffer for name 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 existing FILE_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);}
https://msdn.microsoft.com/en-us/library/aa363875(v=VS.85).aspx
- Creating and Using a Temporary File
- Creating and Using Temporary Tables in Oracle
- Creating and using squashed file systems
- Using and Creating Builds
- Walkthrough : Creating and Using a Dynamic Link Library
- Animating Views Using Scenes and Transitions - Creating a Scene
- [转发]Creating a File Lock on a File Using Jdk1.4
- Writing to a Temporary File
- Creating a sequential file
- Creating a XML Document from scratch without using a file in C#
- Creating and Using a COM Object by Using Visual Studio 2005
- Creating a document centric iPhone/iPad application with own file format using ZipArchive
- Creating and Editing File Templates
- Generate a quick and easy custom pcap file using Python
- Reading and Writing using a Large Random Access File
- Creating a simple Pivot table using LINQ and Telerik RadTreeView for Silverlight
- (转载)攻略:创建和使用动态链接库Walkthrough: Creating and Using a Static Library
- Creating and Using Strong-Named Assemblies
- @1224工作日志
- Servlet之监听器
- 欢迎使用CSDN-markdown编辑器
- C#序列化匿名对象为XML
- Linux进程控制
- Creating and Using a Temporary File
- 一起talk C栗子吧(第八十二回:C语言实例--简单圣诞树)
- Android SDK开发 -- TitleBar重构 (代理模式的使用)
- /etc/ld.so.conf文件
- Hdu 2037 之解题报告
- Ubuntu下为Firefox安装Adobe Flash Player
- Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] No such file or directory’错误的解决方法
- 通过jquery实现页面的动画效果
- sublime text安装ctags定位函数