[VC] large file splitting tool, supporting a maximum size of 3 GB

Source: Internet
Author: User

Pnig0s p.s. In fact, the main thing is to practice the memory ing file method. The Demo is a bit rough, and I used it to separate the SQL file myself.

The usage is clearly written in-h, and the size is calculated in KB. Pay attention to this when using it.

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1G0403b7-0.jpg "/> 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1G0403553-1.jpg "/>

 
 
  1. # Include <stdio. h>
  2. # Include <iostream>
  3. # Include <Windows. h>
  4. # Include <time. h>
  5. # Include "head. h"
  6. Using namespace std;
  7. Int main (int argc, char * argv []) {
  8. HANDLE hInFile = INVALID_HANDLE_VALUE; // remain
  9. HANDLE hInMap;
  10. FILEINFO fi;
  11. BOOL bHelp = FALSE;
  12. DWORD dwPartSize;
  13. For (int I = 0; I <argc; I ++ ){
  14. If (argc = 1 ){
  15. BHelp = TRUE;
  16. }
  17. LPSTR lpChkCmd = argv [I];
  18. LPSTR lpValue = argv [I + 1];
  19. If (lstrcmp (lpChkCmd, "-f") = 0 ){
  20. Fi. lpFileName = lpValue;
  21. } Else if (lstrcmp (lpChkCmd, "-s") = 0 ){
  22. DwPartSize = atol (lpValue) * 1024;
  23. } Else if (lstrcmp (lpChkCmd, "-h") = 0 ){
  24. BHelp = TRUE;
  25. }
  26. }
  27. If (bHelp ){
  28. Printf ("\ n --------------------------------");
  29. Printf ("\ n | \ tLarge file dividing \ t | ");
  30. Printf ("\ n | \ t \ tby: Pnig0s1992 \ t | ");
  31. Printf ("\ n | \ t \ t2011, 11,25 \ t | ");
  32. Printf ("\ n --------------------------------");
  33. Printf ("\ nUsage: \ n"
  34. & Quot; % s-f div.txt-s 1024 \ n & quot"
  35. "Options: \ n"
  36. "-F Specify the file u want to divide. \ n"
  37. "-S Specify the size for each part (KB). \ n"
  38. "-H Display the usage. \ n", argv [0]);
  39. Exit (1 );
  40. }
  41. HInFile = CreateFile (fi. lpFileName, GENERIC_READ | GENERIC_WRITE, file_pai_read | file_write _write, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  42. If (hInFile = INVALID_HANDLE_VALUE ){
  43. Printf ("\ n failed to open the file (% d).", GetLastError ());
  44. Return 0;
  45. }
  46. Fi. dwFileLowSize = GetFileSize (hInFile, & fi. dwFileHighSize );
  47. Fi. dwFileSize = fi. dwFileLowSize;
  48. HInMap = CreateFileMapping (hInFile, NULL, PAGE_READWRITE, 0, 0, NULL );
  49. If (hInMap = NULL ){
  50. Printf ("\ n failed to create source file ing (% d).", GetLastError ());
  51. CloseHandle (hInFile );
  52. Return 0;
  53. }
  54. Fi. dwPartNum = fi. dwFileSize/dwPartSize;
  55. If (fi. dwFileSize % dwPartSize )! = 0 ){
  56. Fi. dwPartNum + = 1;
  57. }
  58. Printf ("\ n file size: % uKB \ t number of parts: % u \ n", fi. dwFileSize/1024, fi. dwPartNum );
  59. DWORD dwCurAddr = 0;
  60. DWORD dwCurSize = 0;
  61. System ("pause ");
  62. DWORD dwBegin, dwFinish;
  63. DwBegin = GetTickCount ();
  64. For (int index = 0; index <fi. dwPartNum; index ++ ){
  65. DwCurSize = fi. dwFileSize-dwCurAddr;
  66. If (dwCurSize> dwPartSize ){
  67. DwCurSize = dwPartSize;
  68. }
  69. LPVOID lpInMapContext;
  70. LpInMapContext = MapViewOfFile (hInMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, dwCurAddr, dwCurSize );
  71. If (! LpInMapContext ){
  72. Printf ("\ n failed to create the source file view. (% d)", GetLastError ());
  73. CloseHandle (hInFile );
  74. Return 0;
  75. }
  76. DwCurAddr + = dwCurSize;
  77. Char cIndex [4];
  78. Itoa (index + 1, cIndex, 10 );
  79. HANDLE hOutFile;
  80. HOutFile = CreateFile (cIndex, GENERIC_ALL, file_pai_read | file_pai_write, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  81. If (hOutFile = INVALID_HANDLE_VALUE ){
  82. Printf ("\ n failed to create the target file (% d).", GetLastError ());
  83. Return 0;
  84. }
  85. HANDLE hOutMap;
  86. HOutMap = CreateFileMapping (hOutFile, NULL, PAGE_READWRITE, 0, dwCurSize, NULL );
  87. If (hOutMap = NULL ){
  88. Printf ("\ n failed to create the target file ing (% d).", GetLastError ());
  89. Return 0;
  90. }
  91. LPVOID lpOutMapContext;
  92. LpOutMapContext = MapViewOfFile (hOutMap, FILE_MAP_WRITE, 0, 0 );
  93. If (hOutMap = NULL ){
  94. Printf ("\ n failed to create the target file view. (% d)", GetLastError ());
  95. Return 0;
  96. }
  97. CopyMemory (lpOutMapContext, lpInMapContext, dwCurSize );
  98. Printf ("\ n [MSG] block % d completed.", index + 1 );
  99. UnmapViewOfFile (lpInMapContext );
  100. UnmapViewOfFile (lpOutMapContext );
  101. CloseHandle (hOutMap );
  102. CloseHandle (hOutFile );
  103. }
  104. CloseHandle (hInMap );
  105. CloseHandle (hInFile );
  106. DwFinish = GetTickCount ();
  107. DWORD dwRunTime = dwFinish-dwBegin;
  108. Printf ("\ n time: % u millisecond", dwRunTime );
  109. Return 1;
  110. }

 

This article is from the "About: Blank H4cking" blog, please be sure to keep this source http://pnig0s1992.blog.51cto.com/393390/724509

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.