c++ implements merging files and splitting instance code
- 2020-06-01 10:45:59
- OfStack
This paper mainly studies the relevant content of c++ implementation of merging files and splitting, and shares the implementation code as follows.
Yesterday, I saw an article about TFS, which is about the use of a distributed architecture to combine small images into large files in order to process massive images
Driven by interest, I wrote a simple file merge splitter
The merge method is simple, writing small files to large files, using base 2. In the large file header, use 1 int to record the number of small files that have been merged in. Next, write the small files block by block.
Each block is divided into three parts. The first part is the name of the file, which is recorded with 20 char. Part 2 USES an int to record the length of small files; Part 3 is the actual content of the small file
This way, when splitting, you get the number of small files by the header, and then get the name, file length, and content one by one
Attach source code
Merger:
void Merge()
{
char name[20];
string str1 = "F:/ data /1.jpg";
string str2 = "F:/ data /2.jpg";
string strBlock = "F:/ data /BLOCK.dat";
FILE* f1 = fopen(str1.c_str(),"rb+");
FILE* f2 = fopen(str2.c_str(),"rb+");
FILE* f3 = fopen(strBlock.c_str(),"rb+");
// Gets the length of the file
int iLen1 =filelength(fileno(f1));
int iLen2 = filelength(fileno(f2));
char *buf1 = new char[iLen1];
memset(buf1,0x0,iLen1);
char *buf2 = new char[iLen2];
memset(buf2,0x0,iLen2);
// Get file content
fread(buf1,iLen1,1,f1);
fread(buf2,iLen2,1,f2);
fclose(f1);
fclose(f2);
// Record the number of merged files in the file header
int iCount = 2;
fseek(f3,0,SEEK_SET);
fwrite(&iCount,sizeof(int),1,f3);
// Write the first 1 A file
memset(name,0x0,20);
strcpy(name,str1.c_str());
fwrite(name,20,1,f3);
fwrite(&iLen1,sizeof(int),1,f3);
fwrite(buf1,iLen1,1,f3);
// Write the first 2 A file
memset(name,0x0,20);
strcpy(name,str2.c_str());
fwrite(name,20,1,f3);
fwrite(&iLen2,sizeof(int),1,f3);
fwrite(buf2,iLen2,1,f3);
fclose(f3);
// Delete the requested memory
delete[] buf1;
delete[] buf2;
}
Resolution:
void Split()
{
char name[20];
string strBlock = "F:/ data /BLOCK.dat";
FILE* f3 = fopen(strBlock.c_str(),"rb+");
// Get number of files
int iCount = 0;
fseek(f3,0,SEEK_SET);
fread(&iCount,sizeof(int),1,f3);
for(int i=0;i<iCount;i++)
{
memset(name,0x0,20);
fread(&name,20,1,f3);
// Name of the output
cout<<name<<endl;
int iLen = 0;
// Read file length
fread(&iLen,sizeof(int),1,f3);
char *buff = new char[iLen];
// Read file contents
fread(buff,iLen,1,f3);
char fileLen[10];
sprintf(fileLen, "%d", iLen);
// Name by file length
string s = fileLen;
string strName = "F:/ data /";
strName += s;
strName += string(".jpg") ;
// new 1 A file
FILE* file= fopen(strName.c_str(),"wb+");
fwrite(buff,iLen,1,file);
fclose(file);
}
fclose(f3);
}
conclusion
That's all for this article about c++ to merge files and split instance code. Interested friends can continue to see the site other related topics, if there are shortcomings, welcome to point out the message. Thank you for your support!