How to split a file into multiple small files

zhaozj2021-02-08  206

How to split a file into multiple small files

Author: Xu Jing Zhou

Download instance code (http://www.9cbs.net/cnshare/soft/6/6235.shtm)

You may have encountered such a problem? When you have a bigger software, you can't copy all of them all by a floppy disk, you may think that it will be broken down, and then you will merge them. There are a lot of split tools now. Do you want to do your own split tool? Let me tell you with a routine that I used to use VC! The interface after the program is running as follows:

Basic composition of ideology: the basic idea of ​​document segmentation is much simpler than the composition of the multi-file merged into a executable>, it mainly is divided into divided files and combined division. The two parts of the document. Split files, allow the original file to simult to the specified split size, and then read it in order to designate the split size data to the respective new files. Merger files, after reading the sequential file sequence, write into a file.

l Segment the file: Open the file, read the specified split size, write into a new file, then read the same size of data, then write into a new file ... until reading the file Part of the data writes to the last new file. For each split new file name, the method of adding digital information before the original file name, according to the segmentation order, pressing a digital identification information to merge.

Some of the code of the division file is implemented as follows:

// File segmentation

INT cfilespltdlg :: splitme ()

{

... (omissions: This part of the code is omitted)

// Split file

Do {

/ / Dynamically create a new number of new file names

Name = _LTOA (L, BUFF, 10);

Name = _t ("_");

Cstring newpath;

// Judgment the selection directory is not tail, there is already "/"

IF (m_targetpath.right (1) == '//')

Newpath = m_targetpath;

Else

NewPath = m_targetpath _t ("//");

IF (! destfile.open (newpa name m_sourcefile.getFileName (),

Cfile :: ModeWrite |

Cfile :: ShareExClusive |

CFILE :: TypeBinary |

CFILE :: MODECREATE, & EX) {

Tchar Szerror [1024];

EX.GETERRORMESSAGE (Szerror, 1024);

:: AfxMessageBox (Szerror);

m_SourceFile.Close ();

Return 1;

}

Do {

dwread = m_sourcefile.read (buffer, ncount);

Destfile.write (buffer, dwread);

} // When the file is smaller than the specified size

While (dwread> 0 && destfile.getlength ()

destfile.close ();

l ;

UpdateWindow ();

WHILE (dwread> 0);

m_SourceFile.Close ();

Return 0;

}

l When the merge file: In contrast to the method used above, after reading each divided small file, press the new file in the new file, according to the order of the digital size before the division, according to its segmentation file, this new file The name, for the file name (both original file name) after the number of files before the division is divided. Some code of the merged file is achieved as follows:

// File merge

Int cfilespltdlg :: mergeme ()

{

... (omissions: This part of the code is omitted)

// Start merge file

Do {

// Automatically locate the digital information before the file name

PREF = _LTOA (L, BUFF, 10);

pref = _t ("_");

// Open a new segmentation file

IF (! m_sourcefile.open (NewPath Pref M_FileName,

CFILE :: ModeRead |

Cfile :: ShareExClusive |

CFILE :: TypeBinary, & ex)) {

Tchar Szerror [1024];

EX.GETERRORMESSAGE (Szerror, 1024);

destfile.close ();

m_path = _t ("");

m_filename = _t ("");

NewPath = _t ("");

Updatedata (FALSE);

Return 0;

}

Else

// form a new file name

Name = _T (NewPath Pref M_FileName);

Do {// Write to the target file

dwread = m_sourcefile.read (buffer, ncount);

Destfile.write (buffer, dwread);

WHILE (dwread> 0);

m_SourceFile.Close ();

l ;

UpdateWindow ();

WHILE (L <500); // little bit dirty solution, but you can always improw it! ...

Return 0;

}

The specific implementation of the above part of the code, please refer to the source code after downloading the routine.

Contact information:

Future Studio (Future Studio)

转载请注明原文地址:https://www.9cbs.com/read-2127.html

New Post(0)