可实现文件的分割与合并
#include <stdio.h> #include <stdlib.h> #include <string.h> int Choice(); void Divide(FILE *fp, int size); void Merge(FILE *fp, int num); int main(int argc, char *argv[]) { int m = Choice(); if(m == 1) { if(argc != 3) { printf("Para must be 3 numbers!\n"); exit(0); } FILE *fp; fp = fopen(argv[1], "rb"); if(fp == NULL) { printf("open %s file failed!\n", argv[1]); exit(0); } int size = atoi(argv[2]); Divide(fp, size); fclose(fp); } else { FILE *fp; fp = fopen(argv[1], "wb");//合并后的文件名 if(fp == NULL) { printf("Open file failed!\n"); exit(0); } int num = atoi(argv[2]);//相应的文件个数 Merge(fp, num); fclose(fp); } return 0; } int Choice() { int m; printf("Welcome come to the V1.0 FILE OPERATE machine!\n"); printf("\n---------------------------------------------\n"); printf("Please input your choice!\n\n"); printf("\t 1.The divide operate!\t \n"); printf("\t 2.The merge operate!\t \n"); while(1) { scanf("%d", &m); if(m!=1 && m!=2) { printf("Please input legal data!\n"); continue; } break; } return m; } void Divide(FILE *fp, int size) { char partname[100]; char ch; int flag = 0; for(int a=1; ;a++) { itoa(a, partname, 10); strcat(partname, ".part"); FILE *out; out = fopen(partname, "wb"); if(out == NULL) { printf("Open %s file failed", partname); exit(0); } for(int i=1; i<=size; i++) { fread(&ch, sizeof(char), 1, fp); if(feof(fp)) { flag = 1; break; } fwrite(&ch, sizeof(char), 1, out); } fclose(out); if(flag == 1) break; } printf("Congratulate you've divide the file!\n"); } void Merge(FILE *fp,int num) { char partname[100]; char str[100]; for(int a=1; a<=num; a++) { itoa(a, partname, 10); strcat(partname, ".part"); FILE *out; out = fopen(partname, "rb"); if(out == NULL) { printf("Open %s file failed\n", partname); exit(0); } while(1) { int r = fread(str, sizeof(char), 100, out); if(r<1) break; fwrite(str, sizeof(char), r, fp); } fclose(out); } printf("Congratulate you've merge the file!\n"); }改编后的程序无需手动输入数量,只需手动输入目录txt名
#include <stdio.h> #include <stdlib.h> #include <string.h> int Choice(); void Divide(FILE *fp, int size, FILE *div); void Merge(FILE *fp, FILE *di); int main(int argc, char *argv[]) { int m = Choice(); if(m == 1) { if(argc != 4) { printf("Para must be 3 numbers!\n"); exit(0); } FILE *fp, *div; fp = fopen(argv[1], "rb"); div = fopen(argv[3], "wb");//用于生成目录 if(fp == NULL || div == NULL) { printf("open file failed!\n"); exit(0); } int size = atoi(argv[2]);//指定分割文件的大小 Divide(fp, size, div); fclose(div); fclose(fp); } else { FILE *fp, *di; fp = fopen(argv[1], "wb");//合并后的文件名 di = fopen(argv[2], "rb");//目录 if(fp == NULL || di == NULL) { printf("Open file failed!\n"); exit(0); } Merge(fp, di); fclose(di); fclose(fp); } return 0; } int Choice() { int m; printf("Welcome come to the V2.0 FILE OPERATE machine!\n"); printf("\n---------------------------------------------\n"); printf("Please input your choice!\n\n"); printf("\t 1.The divide operate!\t \n"); printf("\t 2.The merge operate!\t \n"); while(1) { scanf("%d", &m); if(m!=1 && m!=2) { printf("Please input legal data!\n"); continue; } break; } return m; } void Divide(FILE *fp, int size, FILE *div) { char partname[100]; char ch; int flag = 0; for(int a=1; ;a++) { itoa(a, partname, 10); strcat(partname, ".part"); FILE *out; out = fopen(partname, "wb"); if(out == NULL) { printf("Open file failed\n"); exit(0); } for(int i=1; i<=size; i++) { fread(&ch, sizeof(char), 1, fp); if(feof(fp)) { flag = 1; break; } fwrite(&ch, sizeof(char), 1, out); } fprintf(div,"%s\r\n", partname); fclose(out); if(flag == 1) break; } printf("Congratulate you've divide the file!\n"); } void Merge(FILE *fp, FILE *di) { char partname[100]; char str[100]; fscanf(di, "%s", partname); while( !feof(di)) { FILE *out; out = fopen(partname, "rb"); if(out == NULL) { printf("Open %s file failed\n", partname); exit(0); } while(1) { int r = fread(str, sizeof(char), 100, out); if(r<1) break; fwrite(str, sizeof(char), r, fp); } fclose(out); fscanf(di, "%s", partname); } printf("Congratulate you've merge the file!\n"); }