API太多时间久了容易忘记,存在这儿,用的时候免得到处查找浪费时间。
//--------------------------------------------------------------- /* * WinAPI文件操作 * Compiled: VC2005 * Character Set: Multi-Byte * Auth: 陈相礼 * Data: 2010/05/27 */ //--------------------------------------------------------------- #include <windows.h> #include <iostream> #define BUFSIZE 4096 // WinAPI文件拷贝 using namespace std; void main( int argc, char **argv ) { HANDLE hFile; HANDLE hAppend; //long dwRet; //FILETIME ftCreateTime, ftLastAccessTime, ftLastWriteTime; DWORD dwBytesRead, dwBytesWritten, dwPos; char buff[BUFSIZE]; if ( argc != 3 ) { cout << "Parameters error!/nUsage: " << argv[0] << " Source Destination" << endl; return; } // 共享读取的方式打开 hFile = CreateFile( argv[1], GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if ( hFile == INVALID_HANDLE_VALUE ) { cout << "Open Source file error!" << endl; return; } // 获取文件的创建日期、上一次访问时间和最后一次更新时间[测试之用] // dwRet = GetFileTime( hFile, &ftCreateTime, &ftLastAccessTime, &ftLastWriteTime ); // 独占写入方式打开 hAppend = CreateFile( argv[2], GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( hAppend == INVALID_HANDLE_VALUE ) { CloseHandle( hFile ); cout << "Open destination file error!" << endl; return; } do {// 文件拷贝操作 if ( ReadFile( hFile, buff, BUFSIZE, &dwBytesRead, NULL ) ) { dwPos = SetFilePointer( hAppend, 0, NULL, FILE_END ); LockFile( hAppend, dwPos, 0, dwPos + dwBytesRead, 0 ); WriteFile( hAppend, buff, dwBytesRead, &dwBytesWritten, NULL ); UnlockFile( hAppend, dwPos, 0, dwPos + dwBytesRead, 0 ); } }while ( dwBytesRead == BUFSIZE ); CloseHandle( hFile ); CloseHandle( hAppend ); }