How to split a file into multiple small files

Source: Internet
Author: User


You may encounter such a problem? When you have a large software that cannot be copied from a floppy disk, you may think of splitting it and copying it back, then combine them. Nowadays, there are many such splitting tools. Do you want to develop a proper splitting tool by yourself? Next let me use a <pocket file splitter> routine that I used to use VC to tell you! After running the program, the interface is as follows:




Figure 1


The idea of basic composition: the basic idea of file segmentation is much simpler than the idea of how to combine multiple files into an executable program, it is mainly divided into two parts: Split files and merged split files. Split the file, divide the original file by the specified split size, read the data of the specified split size in sequence, and write it to the new file. Merge files, read the Separated Files sequentially, and write them into a single file. When splitting a file, open the file, read the specified split-size data segment, write it to a new file, then read a piece of data of the same size, and then write it into a new file ......, Write the last part of the data to the last new file. For each new split file name, add the numeric information before the original file name, in the order of segmentation, add the numerical identification information to each one for use in combination.


The code for splitting a file is as follows:


File: // fileNumber of Parts


Int CFileSpltDlg: SplitMe ()

{

......

File: // minuteCut files

Do {

File: //State to create a number before the new file name

Name = _ ltoa (l, buff, 10 );

Name + = _ T ("_");

CString newpath;


File: // judgeIndicates whether a "" character exists before the end of the selected directory.

If (m_targetpath.Right (1) == \)

Newpath = m_targetpath;

Else

Newpath = m_targetpath + _ T ("\");

If (! DestFile. Open (newpath + name + m_SourceFile.GetFileName (),

Cfile: modeWrite |

Cfile: Export exclusive |

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 size is smaller than the specified size to be split

While (dwRead> 0 & destFile. GetLength () <newlen );

DestFile. Close ();


L ++;

UpdateWindow ();

} While (dwRead> 0 );

M_SourceFile.Close ();

Return 0;

}





When merging files: Unlike the method used in the preceding split, after reading each split small file, follow the order of the numbers before the split file name, write each file to the new file. The name of the new file is the file name after removing the number before the split file (the original file name ).


The code for merging files is as follows:


// File merging function



Int CFileSpltDlg: MergeMe ()

{

......

File: // openMerge files

Do {

File: // fromSpecifies the number before the split file name.

Pref = _ ltoa (l, buff, 10 );

Pref + = _ T ("_");

File: //Open a new split File

If (! M_SourceFile.Open (newpath + pref + m_filename,

Cfile: modeRead |

Cfile: Export exclusive |

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

File: //Into 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 improve it !...


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.