文件映射内存地址.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "ConsoleApplication1.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #endif
  8. #define TFILE _T("\\\\it-server\\IT软件区$\\K刻录驱动\\刻录驱动.rar")
  9. #define TFILE2 _T("H:\\C# WPF 基础教程2.zip")
  10. // 唯一的应用程序对象
  11. CWinApp theApp;
  12. using namespace std;
  13. int main()
  14. {
  15. int nRetCode = 0;
  16. HMODULE hModule = ::GetModuleHandle(nullptr);
  17. if (hModule != nullptr)
  18. {
  19. // 初始化 MFC 并在失败时显示错误
  20. if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0))
  21. {
  22. // TODO: 更改错误代码以符合您的需要
  23. wprintf(L"错误: MFC 初始化失败\n");
  24. nRetCode = 1;
  25. }
  26. else
  27. {
  28. DeleteFile(TFILE2);
  29. // TODO: 在此处为应用程序的行为编写代码。
  30. DWORD dwTickCount = GetTickCount();
  31. #ifndef F2// 内存映射读文件;
  32. HANDLE hFile = NULL;
  33. HANDLE hFileMap = NULL;
  34. LARGE_INTEGER liResult;
  35. hFile = CreateFile(TFILE, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  36. if (INVALID_HANDLE_VALUE == hFile)
  37. {
  38. goto __TestEnd;
  39. }
  40. // 创建文件映射;
  41. hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
  42. if (NULL == hFileMap)
  43. {
  44. goto __TestEnd;
  45. }
  46. //得到系统分配粒度;
  47. SYSTEM_INFO si;
  48. GetSystemInfo(&si);
  49. DWORD dwSysGran = si.dwAllocationGranularity;
  50. //得到文件大小;
  51. LARGE_INTEGER lFileSize;
  52. GetFileSizeEx(hFile, &lFileSize);
  53. CloseHandle(hFile);
  54. hFile = INVALID_HANDLE_VALUE;
  55. LPBYTE lpData = (LPBYTE)MapViewOfFile(hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
  56. FILE* fp = NULL;
  57. fp = _tfopen(TFILE2, _T("ab+"));
  58. fwrite(lpData, lFileSize.QuadPart, 1, fp);
  59. fclose(fp);
  60. // 取消文件映射;
  61. UnmapViewOfFile(lpData);
  62. #if 0
  63. //性能方面,都知道容器大小了就先初始化大小,免得用vector自己的扩容机制浪费CPU
  64. //vecTime.resize(200, 0);
  65. char szPath[MAX_PATH] = { 0 };
  66. for (int i = 1; i <= 200; ++i)
  67. {
  68. __int64 qwFileOffset = 0;
  69. __int64 qwFileSize = lFileSize.QuadPart;
  70. DWORD dwDataLen = 0;
  71. FILE* fp = NULL;
  72. DWORD dwBlockBytes = i*dwSysGran;
  73. if (lFileSize.QuadPart < dwBlockBytes)
  74. dwBlockBytes = lFileSize.QuadPart;
  75. sprintf(szPath, "d:\\test\\%d.zip", i);
  76. //DeleteFileA(pFile);
  77. {
  78. //PERFOR_TEST("复制文件测试4KB_1");
  79. //CPerforTest test(&liResult);
  80. fp = fopen(szPath, "ab+");
  81. while (qwFileSize > 0)
  82. {
  83. dwDataLen = qwFileSize < dwBlockBytes ? qwFileSize : dwBlockBytes;
  84. LPBYTE lpData = (LPBYTE)MapViewOfFile(hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, (DWORD)(qwFileOffset >> 32), (DWORD)(qwFileOffset & 0xffffffff), dwDataLen);
  85. if (NULL == lpData)
  86. break;
  87. //把文件复制到另一个目录下,写文件操作
  88. //fp = fopen(szPath, "ab+");//追加方式写入文件,不存在则创建
  89. fwrite(lpData, dwDataLen, 1, fp);
  90. //fclose(fp);
  91. UnmapViewOfFile(lpData);
  92. qwFileOffset += dwDataLen;
  93. qwFileSize -= dwDataLen;
  94. }
  95. fclose(fp);
  96. }
  97. //vecTime[i - 1] = liResult.LowPart / 300;
  98. Sleep(100);
  99. }
  100. #endif
  101. __TestEnd:
  102. DWORD dwError = GetLastError();
  103. //内核句柄清理工作
  104. if (hFile != INVALID_HANDLE_VALUE)
  105. {
  106. CloseHandle(hFile);
  107. hFile = INVALID_HANDLE_VALUE;
  108. }
  109. if (hFileMap)
  110. {
  111. CloseHandle(hFileMap);
  112. hFileMap = NULL;
  113. }
  114. #else
  115. FILE *fp = nullptr;
  116. fp = _tfopen(TFILE, _T("r"));
  117. if (fp)
  118. {
  119. // 获取文件大小;
  120. if (0 == fseek(fp, 0, SEEK_END))
  121. {
  122. size_t len = ftell(fp);
  123. fseek(fp, 0, SEEK_SET);
  124. BYTE *pData = new BYTE[len];
  125. memset(pData, 0, len);
  126. fread(pData, len, 1, fp);
  127. fclose(fp);
  128. FILE *fp2 = nullptr;
  129. fp2 = _tfopen(TFILE2, _T("ab+"));
  130. if (fp2)
  131. {
  132. fwrite(pData, len, 1, fp2);
  133. fclose(fp2);
  134. }
  135. delete[]pData;
  136. }
  137. }
  138. #endif
  139. printf("%ld\n", GetTickCount() - dwTickCount);
  140. }
  141. }
  142. else
  143. {
  144. // TODO: 更改错误代码以符合您的需要
  145. wprintf(L"错误: GetModuleHandle 失败\n");
  146. nRetCode = 1;
  147. }
  148. system("pause");
  149. return nRetCode;
  150. }