文件合并c程序

/****************INCLUDE********************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
/****************DEFINE*********************/

#define OFFSET   0x70000
/****************FUNCTION*******************/

unsigned int getfilesize(char *file)
{
	unsigned int result;
	struct stat buf;
	result = stat(file,&buf);

	if(result == 0){
		return buf.st_size;
	}else{
		return 0;
	}
}

int main(unsigned int argc,char **argv)
{
	FILE *soc_file1,*soc_file2,*dest_file;
	long count = 0;
	unsigned char tmp;
	unsigned char *SrcFile1,*SrcFile2,*DstFile;
	unsigned long offset;
	unsigned long currpos;

	if(argc<4){
		printf("Usage:CombineBin a.bin b.bin c.bin offset or Usage:CombineBin a.bin b.bin c.bin\n");
		return 0;
	}
	SrcFile1 = argv[1];
	SrcFile2 = argv[2];
	DstFile =argv[3];
	if(argv[4]==NULL)
		offset=OFFSET;
	else
		offset= strtol(argv[4],NULL,16);

	if((soc_file1 = fopen(SrcFile1,"rb")) == NULL){
		printf("Can't open %s\n",SrcFile1);
		exit(0);
	}

	if((soc_file2 = fopen(SrcFile2,"rb")) == NULL){
		printf("Can't open %s\n",SrcFile2);
		exit(0);
	}
	
	dest_file = fopen(DstFile,"w+");
	if(NULL == dest_file){
		printf("Can't open %s\n",DstFile);
		exit(0);
	}

	count = 0;
	tmp = fgetc(soc_file1);
	while(!feof(soc_file1)){
		fputc(tmp,dest_file);
		tmp = fgetc(soc_file1);
	}

	fseek(dest_file, 0, SEEK_END);
	currpos = ftell(dest_file);
	
	if(currpos > offset){
		printf("the offset is flase\n");
		exit(0);
	}
	while(currpos < offset){
		 fputc(0,dest_file);
		 currpos = ftell(dest_file);
	}
	tmp = fgetc(soc_file2);
	while(!feof(soc_file2)){
		fputc(tmp,dest_file);
		tmp = fgetc(soc_file2);
	}
	close(dest_file);
	close(soc_file1);
	close(soc_file2);
	
}

/*********************END OF FILE**********************************/


阅读更多
个人分类: c
想对作者说点什么? 我来说一句

ShapeFileMerge

2008年05月08日 267KB 下载

文件合并程序(C语言)

2011年03月30日 3KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭