Creation, deletion, and maintenance of files

xiaoxiao2021-03-05  21

The application can use the getTemPFileName () function to get the file name of the temporary file, and getTemppath () will return the path to the directory where the temporary file is created.

The following example copies a file to another file. The target file will override the content in the source file.

The application uses createfile () Opens the file original.txt. Then, the application uses the GetTempFileName () function to get the file name of the temporary file, and use createFile () to create a temporary file. The application reads 4K content from the source file and puts it into the buffer, and then change the content to uppercase in the buffer, and then write the modified content to the temporary file. When the source file is converted and written to the temporary file, the application will turn off these two files and then rename the temporary file with allCaps.txt with the movefileex () function.

#include

#include

#define buffsize 4096

int main () {HANDLE hFile; HANDLE hTempFile; DWORD dwBytesRead, dwBytesWritten, dwBufSize = BUFSIZE; char szTempName [MAX_PATH]; char buffer [BUFSIZE]; char lpPathBuffer [BUFSIZE]; // open the source file hFile = CreateFile ( "original. TXT ", // file name generic_read, // read file 0, // does not share null, // default security attribute open_existing, file_attribute_normal, null); // Do not use temporary file IF (hfile == invalid_handle_value) {printf (" Could Not Open File. "); Return 0;} // Get a temporary file path

GetTemppath (dwbufsize, lppathbuffer);

// Create a temporary file getTempFileName (LPPathBuffer, // Temporary file directory "new", // temporary file prefix 0, // creates a unique name sztempname); / / Save the name of the buffer

HTEMPFILE = CREATEFILE ((lptstr) sztempname, // file name generic_read | generic_write, // Used to read and write operation 0, // Do not share null, // default security attribute create_always, // Override file file_attribute_normal, null );

转载请注明原文地址:https://www.9cbs.com/read-33189.html

New Post(0)