先写个简单的文件合并函数(非常简单,贴上来纯粹为了给记忆中加点东西,看到的人不要骂我),有时间写个捆绑的程序
//
CommbineFile.cpp : 定义控制台应用程序的入口点。
//
// #include "stdafx.h"
#include " stdio.h "
#include " stdlib.h "
#include " windows.h "
bool CombineFile( char * sourceFile, char * attachFile, char * destFile)
{
FILE * fdSourceFile = NULL;
FILE * fdAttachFile = NULL;
int nSourceFileSize = 0 ,nAttachFileSize = 0 ;
fdSourceFile = fopen(sourceFile, " r " );
fdAttachFile = fopen(attachFile, " r " );
if ( ! fdSourceFile || ! fdAttachFile)
{
return false ;
}
if (fseek(fdSourceFile, 0L ,SEEK_END))
{
return false ;
}
nSourceFileSize = ftell(fdSourceFile);
fseek(fdSourceFile, 0L ,SEEK_SET);
if (fseek(fdAttachFile, 0L ,SEEK_END))
{
return false ;
}
nAttachFileSize = ftell(fdAttachFile);
fseek(fdAttachFile, 0L ,SEEK_SET);
char * sBuf = ( char * )malloc((nSourceFileSize + 1 ) * sizeof ( char ));
if ( ! sBuf)
{
return false ;
}
char * aBuf = ( char * )malloc((nAttachFileSize + 1 ) * sizeof ( char ));
if ( ! aBuf)
{
return false ;
}
memset(sBuf, 0 ,nSourceFileSize + 1 ); // not need,I think
memset(aBuf, 0 ,nAttachFileSize + 1 );
int nSourceRead = 0 ,nAttachRead = 0 ;
nSourceRead = fread(sBuf, sizeof ( char ),nSourceFileSize,fdSourceFile);
nAttachRead = fread(aBuf, sizeof ( char ),nAttachFileSize,fdAttachFile);
FILE * fdDestFile = fopen(destFile, " w " );
if ( ! fdDestFile)
{
return false ;
}
fwrite(sBuf, sizeof ( char ),nSourceFileSize,fdDestFile);
fwrite(aBuf, sizeof ( char ),nAttachFileSize,fdDestFile);
fclose(fdAttachFile);
fclose(fdDestFile);
fclose(fdSourceFile);
free(sBuf);
free(aBuf);
return true ;
}
int main( int argc, char * argv[])
{
CombineFile( " D:\\1.txt " , " D:\\2.txt " , " D:\\3.txt " );
return 0 ;
}
//
// #include "stdafx.h"
#include " stdio.h "
#include " stdlib.h "
#include " windows.h "
bool CombineFile( char * sourceFile, char * attachFile, char * destFile)
{
FILE * fdSourceFile = NULL;
FILE * fdAttachFile = NULL;
int nSourceFileSize = 0 ,nAttachFileSize = 0 ;
fdSourceFile = fopen(sourceFile, " r " );
fdAttachFile = fopen(attachFile, " r " );
if ( ! fdSourceFile || ! fdAttachFile)
{
return false ;
}
if (fseek(fdSourceFile, 0L ,SEEK_END))
{
return false ;
}
nSourceFileSize = ftell(fdSourceFile);
fseek(fdSourceFile, 0L ,SEEK_SET);
if (fseek(fdAttachFile, 0L ,SEEK_END))
{
return false ;
}
nAttachFileSize = ftell(fdAttachFile);
fseek(fdAttachFile, 0L ,SEEK_SET);
char * sBuf = ( char * )malloc((nSourceFileSize + 1 ) * sizeof ( char ));
if ( ! sBuf)
{
return false ;
}
char * aBuf = ( char * )malloc((nAttachFileSize + 1 ) * sizeof ( char ));
if ( ! aBuf)
{
return false ;
}
memset(sBuf, 0 ,nSourceFileSize + 1 ); // not need,I think
memset(aBuf, 0 ,nAttachFileSize + 1 );
int nSourceRead = 0 ,nAttachRead = 0 ;
nSourceRead = fread(sBuf, sizeof ( char ),nSourceFileSize,fdSourceFile);
nAttachRead = fread(aBuf, sizeof ( char ),nAttachFileSize,fdAttachFile);
FILE * fdDestFile = fopen(destFile, " w " );
if ( ! fdDestFile)
{
return false ;
}
fwrite(sBuf, sizeof ( char ),nSourceFileSize,fdDestFile);
fwrite(aBuf, sizeof ( char ),nAttachFileSize,fdDestFile);
fclose(fdAttachFile);
fclose(fdDestFile);
fclose(fdSourceFile);
free(sBuf);
free(aBuf);
return true ;
}
int main( int argc, char * argv[])
{
CombineFile( " D:\\1.txt " , " D:\\2.txt " , " D:\\3.txt " );
return 0 ;
}