#include <stdio.h> #include <Windows.h> //调用Windows API需要加此头文件 int main(int argc, char *argv[]) { HANDLE hFile; HANDLE hAppend; //声明句柄变量 DWORD dwBytesRead, dwBytesWritten, dwPos; //DWORD等同于无符号的int BYTE buff[4096]; //声明数组,该数组申请的内存空间为4096个字节 hFile = CreateFile(TEXT("E:\\file1\\file1_1.txt"), //注意Visual环境下路径的单个\为转义字符 GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //注意被调用函数的参数个数不能少 if (hFile == INVALID_HANDLE_VALUE) { printf("Could not open file1_1.txt."); return 0; //返回值的类型要与调用其函数的类型相同 } hAppend = CreateFile(TEXT("E:\\file1\\file1_2.txt"), FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hAppend == INVALID_HANDLE_VALUE) { printf("Could not open file1_2.txt."); return 0; } while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL) && dwBytesRead > 0) { 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); } CloseHandle(hFile); CloseHandle(hAppend); return 0; }