标签:文件的分割与合并
/*文件的分割*/ #include<stdio.h> #include<stdlib.h> #include<string.h> #define NUM 1024 * 1024 * 100 int main() { //所需分割的文件路径 FILE *fr = fopen("F:\\PL.exe", "rb"); if (fr == NULL) { exit(-1); } char *buff = (char*)malloc(sizeof(char) * NUM); if (buff == NULL) { exit(-2); } size_t rs = 0; FILE *fw;// = fopen("F:\\1.exe", "wb"); //将分割的文件循环存放在目标路径下 char str[100] = "F:\\"; char tmp_file[100]; char name[100]; int i = 1; while((rs = fread(buff,sizeof(char),NUM, fr)) > 0) { // sprintf itoa(i,name,10);// i->1-->name-->"1" strcpy(tmp_file, str);// tmp_file ->>"f:\\" strcat(tmp_file,name);// f:\\1 strcat(tmp_file, ".exe"); fw = fopen(tmp_file, "wb"); fwrite(buff,sizeof(char) ,rs, fw); fclose(fw); i++; } fclose(fr); return 0; } /*文件的合并*/ #include<stdio.h> #include<stdlib.h> #include<string.h> #define NUM 1024*1024*100// 10MB int main() { FILE *fr1 = fopen("F:\\1.exe", "rb"); //分割后的文件路径 FILE *fr2 = fopen("F:\\2.exe", "rb"); FILE *fw = fopen("F:\\meger.exe", "wb");//文件合并后的目标路径 char *buff = (char*)malloc(sizeof(char) * NUM); // size_t rs = 0; FILE *arr[] = {fr1,fr2}; for (int i=0;i<2;i++) { while((rs = fread(buff,sizeof(char),NUM, arr[i])) > 0) { fwrite(buff, sizeof(char), rs, fw); } fclose(arr[i]); } fclose(fw); free(buff); return 0; }
标签:文件的分割与合并
原文地址:http://ffsust.blog.51cto.com/10962735/1720151