文件追写:文件 ""E:\one.txt.log"" 写到""E:\two.txt.log""
来源:互联网 发布:手机怎样升级4g网络 编辑:程序博客网 时间:2024/06/16 20:52
#include <windows.h>#include<stdio.h>int main(){ HANDLE hFile; HANDLE hAppend; DWORD dwBytesRead, dwBytesWritten, dwPos; BYTE buff[4096];// Open the existing file.hFile = CreateFile(TEXT("E:\\one.txt.log"), // open One.txtGENERIC_READ, // open for reading0, // do not shareNULL, // no securityOPEN_EXISTING, // existing file onlyFILE_ATTRIBUTE_NORMAL, // normal fileNULL); // no attr. templateif (hFile == INVALID_HANDLE_VALUE){printf("Could not open One.txt.");return 0;}// Open the existing file, or if the file does not exist,// create a new file.hAppend = CreateFile(TEXT("E:\\two.txt.log"), // open Two.txtGENERIC_WRITE, // open for writingFILE_SHARE_READ, // allow multiple readersNULL, // no securityOPEN_ALWAYS, // open or createFILE_ATTRIBUTE_NORMAL, // normal fileNULL); // no attr. templateif (hAppend == INVALID_HANDLE_VALUE){printf("Could not open Two.txt.");return 0;}// Append the first file to the end of the second file.// Lock the second file to prevent another process from// accessing it while writing to it. Unlock the// file when writing is finished.do{if (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL)){dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);LockFile(hAppend, dwPos, 0, dwBytesRead, 0);WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL);UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);}} while (dwBytesRead == sizeof(buff));// Close both files.CloseHandle(hFile);CloseHandle(hAppend);return 1;}