123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "ConsoleApplication1.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- #define TFILE _T("\\\\it-server\\IT软件区$\\K刻录驱动\\刻录驱动.rar")
- #define TFILE2 _T("H:\\C# WPF 基础教程2.zip")
- // 唯一的应用程序对象
- CWinApp theApp;
- using namespace std;
- int main()
- {
- int nRetCode = 0;
- HMODULE hModule = ::GetModuleHandle(nullptr);
- if (hModule != nullptr)
- {
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- wprintf(L"错误: MFC 初始化失败\n");
- nRetCode = 1;
- }
- else
- {
- DeleteFile(TFILE2);
- // TODO: 在此处为应用程序的行为编写代码。
- DWORD dwTickCount = GetTickCount();
- #ifndef F2// 内存映射读文件;
- HANDLE hFile = NULL;
- HANDLE hFileMap = NULL;
- LARGE_INTEGER liResult;
- hFile = CreateFile(TFILE, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (INVALID_HANDLE_VALUE == hFile)
- {
- goto __TestEnd;
- }
- // 创建文件映射;
- hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
- if (NULL == hFileMap)
- {
- goto __TestEnd;
- }
- //得到系统分配粒度;
- SYSTEM_INFO si;
- GetSystemInfo(&si);
- DWORD dwSysGran = si.dwAllocationGranularity;
- //得到文件大小;
- LARGE_INTEGER lFileSize;
- GetFileSizeEx(hFile, &lFileSize);
- CloseHandle(hFile);
- hFile = INVALID_HANDLE_VALUE;
- LPBYTE lpData = (LPBYTE)MapViewOfFile(hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
- FILE* fp = NULL;
- fp = _tfopen(TFILE2, _T("ab+"));
- fwrite(lpData, lFileSize.QuadPart, 1, fp);
- fclose(fp);
- // 取消文件映射;
- UnmapViewOfFile(lpData);
- #if 0
- //性能方面,都知道容器大小了就先初始化大小,免得用vector自己的扩容机制浪费CPU
- //vecTime.resize(200, 0);
- char szPath[MAX_PATH] = { 0 };
- for (int i = 1; i <= 200; ++i)
- {
- __int64 qwFileOffset = 0;
- __int64 qwFileSize = lFileSize.QuadPart;
- DWORD dwDataLen = 0;
- FILE* fp = NULL;
- DWORD dwBlockBytes = i*dwSysGran;
- if (lFileSize.QuadPart < dwBlockBytes)
- dwBlockBytes = lFileSize.QuadPart;
- sprintf(szPath, "d:\\test\\%d.zip", i);
- //DeleteFileA(pFile);
- {
- //PERFOR_TEST("复制文件测试4KB_1");
- //CPerforTest test(&liResult);
- fp = fopen(szPath, "ab+");
- while (qwFileSize > 0)
- {
- dwDataLen = qwFileSize < dwBlockBytes ? qwFileSize : dwBlockBytes;
- LPBYTE lpData = (LPBYTE)MapViewOfFile(hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, (DWORD)(qwFileOffset >> 32), (DWORD)(qwFileOffset & 0xffffffff), dwDataLen);
- if (NULL == lpData)
- break;
- //把文件复制到另一个目录下,写文件操作
- //fp = fopen(szPath, "ab+");//追加方式写入文件,不存在则创建
- fwrite(lpData, dwDataLen, 1, fp);
- //fclose(fp);
- UnmapViewOfFile(lpData);
- qwFileOffset += dwDataLen;
- qwFileSize -= dwDataLen;
- }
- fclose(fp);
- }
- //vecTime[i - 1] = liResult.LowPart / 300;
- Sleep(100);
- }
- #endif
- __TestEnd:
- DWORD dwError = GetLastError();
- //内核句柄清理工作
- if (hFile != INVALID_HANDLE_VALUE)
- {
- CloseHandle(hFile);
- hFile = INVALID_HANDLE_VALUE;
- }
- if (hFileMap)
- {
- CloseHandle(hFileMap);
- hFileMap = NULL;
- }
- #else
- FILE *fp = nullptr;
- fp = _tfopen(TFILE, _T("r"));
- if (fp)
- {
- // 获取文件大小;
- if (0 == fseek(fp, 0, SEEK_END))
- {
- size_t len = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- BYTE *pData = new BYTE[len];
- memset(pData, 0, len);
- fread(pData, len, 1, fp);
- fclose(fp);
- FILE *fp2 = nullptr;
- fp2 = _tfopen(TFILE2, _T("ab+"));
- if (fp2)
- {
- fwrite(pData, len, 1, fp2);
- fclose(fp2);
- }
- delete[]pData;
- }
- }
- #endif
- printf("%ld\n", GetTickCount() - dwTickCount);
- }
- }
- else
- {
- // TODO: 更改错误代码以符合您的需要
- wprintf(L"错误: GetModuleHandle 失败\n");
- nRetCode = 1;
- }
- system("pause");
- return nRetCode;
- }
|