文件合并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**********************************/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值