File splitting Tool

Source: Internet
Author: User

# Include <iostream> # include <iomanip> # include <fstream> # include <string> using namespace std; int main () {string path; cout <"Enter the path of the file to be separated:"; cin> path; string block; cout <"Enter the file size of each block (1G/1 M/1 K/1B):"; cin> block; const int block_size = 33554432; unsigned _ int64 size; int ubound = block. size ()-1; sscanf (block. substr (0, ubound ). c_str (), "% I64d", & size); switch (block [ubound]) {case 'G': {size <= 30; break ;} case 'M': {size <= 20; Break;} case 'K': {size <= 10; break;} default: {break;} ifstream in (path. c_str (), ios: binary); if (! In) {cout <"failed to open the file. Please run it again! "<Endl; return-1;} int count = 0; double sum = 0; char * buf = new char [block_size]; ofstream out (path + ". bat "). c_str (); out <"@ echo off" <endl; out <"echo ---------------------------------------------------" <endl; out <"echo-welcome to the file splitting tool-" <endl; out <"echo-" <endl; out <"echo--" <endl; out <"echo -----------------------------------------------------" <endl; for (int I = 1; I> 0; I ++, count ++) {itoa (I, buf, 10); st Ring fullname = path + ". part "+ buf; cout <" generating "<fullname <endl; ofstream fout (fullname. c_str (), ios: binary | ios: trunc); unsigned _ int64 current_size = size; while (current_size> 0) {if (current_size> = block_size) {in. read (buf, block_size);} else {in. read (buf, current_size);} if (in. gcount () = 0) {I =-1; break;} current_size-= in. gcount (); sum + = in. gcount (); fout. write (buf, in. gcount ();} fout. close () ;}out <"echo this batch of processed files are divided "<Count <" sub-files, total "; if (sum> = 1000000000) {out <fixed <setprecision (2) <sum/1073741824 <"G";} else if (sum> = 1000000) {out <fixed <setprecision (2) <sum/1048576 <"M";} else if (sum> = 1000) {out <fixed <setprecision (2) <sum/1024 <"K" ;}else {out <sum <"B" ;}out <endl; out <"echo: 1. Check whether the number of sub-files is consistent and placed in the current directory "<endl; out <" echo 2. whether the available space is sufficient "<endl; out <"pause" <endl; out <"echo starts file merging. Do not close the merging process. The merging duration is related to the file size." <endl; Out <"the current window is automatically closed after echo merge ends" <endl; out <"echo wait... "<endl; out <" copy/B "; int pos = path. find_last_of ('\'); string filename = path. substr (pos + 1); for (I = 0; I <count; I ++) {out <filename <". part "<I + 1; if (I <count-1) {out <'+';} out <'' <filename <endl; out. close (); delete [] buf; in. close (); cout <"file split successful! "<Endl; system (" pause "); return 0 ;}

 

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.