?? merge.h
字號:
#ifndef MERGE_H
#define MERGE_H
//以下為文件合并與分解函數(shù),合并文件后的文件中各子文件順序存放,格式為:
//文件名長度(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 + -