📄 merge.h
字号:
#ifndef MERGE_H
#define MERGE_H
//以下为文件合并与分解函数,合并文件后的文件中各子文件顺序存放,格式为:
//文件名长度(int)、文件名、文件长度(int)、文件内容
//---------------------------------------------------------------------------
//将文件SoruceFile合并到文件DestFile中
#include <windows.h>
#include <fstream>
#include <strsafe.h>
#include <direct.h>
#include <io.h>
#define BUFSIZE MAX_PATH
bool MergeFile(LPTSTR SourceFile,LPTSTR DestFile);
bool SplitFile(LPTSTR SourceFile,LPTSTR DestDir);
void check_make_path(char file_name[]);
using namespace std;
/*
int main()
{
LPTSTR namelist[3];
namelist[0]=(LPTSTR)malloc(BUFSIZE);
namelist[1]=(LPTSTR)malloc(BUFSIZE);
namelist[2]=(LPTSTR)malloc(BUFSIZE);
wcscpy( namelist[0], L"E:\\test\\1.txt" );
wcscpy( namelist[1], L"E:\\test\\2.exe" );
wcscpy( namelist[2], L"E:\\test\\x.dat" );
//MergeFile(namelist[0],namelist[2]);
//MergeFile(namelist[1],namelist[2]);
SplitFile(namelist[2],L"E:\\test\\test");
}
*/
bool MergeFile(LPTSTR SourceFile,LPTSTR DestFile)
{
char iSize;
char*name=(char*)malloc(256);
ifstream sourfile;
sourfile.open(SourceFile,ios::binary);
ofstream destfile;
destfile.open(DestFile,ios::app|ios::binary);
//destfile.seekp(0,ios::end);//指针移到文件尾
iSize=wcslen(SourceFile);//文件名长度
destfile.put(iSize);//写文件名长度
wcstombs( name, SourceFile, iSize+1) ;
//destfile.write((char*)SourceFile,iSize);//写文件名
for (int i=0;i<(int)iSize;i++)
{
destfile.put(name[i]); //写文件名
printf("%d",i);
printf("%c\n",name[i]);
}
sourfile.seekg(0,ios::end);
unsigned long length;
length=sourfile.tellg(); //文件长度
printf("size%ul\n",length);
destfile.write((char*)&length,4);
//destfile.put(iSize);//写文件长度
sourfile.seekg(0,ios::beg);
sourfile.get(iSize);
while (!sourfile.eof())
{
destfile.put(iSize);
sourfile.get(iSize);
}
sourfile.close();
destfile.close();
return true;
}
bool SplitFile(LPTSTR SourceFile ,LPTSTR DestDir )
{
char ch;
ifstream sourfile;
sourfile.open(SourceFile,ios::binary);
sourfile.seekg(0,ios::beg);
sourfile.get(ch);
int iSize=wcslen(DestDir);
while (!sourfile.eof())
{
CHAR*temp=(CHAR*)malloc(256);
CHAR*temp2=(CHAR*)malloc(256);
CHAR*temp3=(CHAR*)malloc(256);
for (int i=0;i<256;i++)
{
temp[i]='\0'; //源文件全路径like E:\test\1.txt.var
temp2[i]='\0';//源文件相对路径like \test\1.txt.var
temp3[i]='\0';//目标文件全路径
}
int len=ch;
for ( int i=0;i<len;i++)
{
sourfile.get(ch);
temp[i]=ch;
if (i>=2)
temp2[i-2]=ch;
}
wcstombs( temp3, DestDir, iSize) ;//temp此时为char* 类型文件目录
strcat(temp3,temp2);
unsigned long length;
sourfile.read((char*)&length,4);
// len=ch;
ofstream destfile;
check_make_path(temp3);
destfile.open( temp3,ios_base::app|ios_base::binary);
for (int i=0;i<length;i++)
{
sourfile.get(ch);
destfile.put(ch);
printf ("%l",i/len);
}
destfile.close();
sourfile.get(ch);
printf ("%s Splited \n", temp3);
free(temp);
free(temp2);
free(temp3);
}
return true;
}
void check_make_path(char file_name[]) {
char*t = file_name;
while (t = strchr(++t, '\\')) {
*t = 0;
if (access(file_name, 0) != -1) {
*t = '\\';
continue;
}
_mkdir(file_name);
*t = '\\';
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -