可实现文件的分割与合并
#include
#include
#include
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
#include
#include
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");
}
#include
#include
#include
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, *mod;
fp = fopen(argv[1], "wb");//合并后的文件名
mod = fopen(argv[2], "rb");//实现排序
char s[10000][20];
char str[20];
int a[10000];
int k=0;
fscanf(mod, "%s", s[0]);
while( !feof(mod))
{
sscanf(s[k], "%[^.]", str);
a[k] = atoi(str);
fscanf(mod, "%s", s[++k]);
}
int n=k;
for(int i=0;i1;i++)
{
for(int j=i+1;jif(a[i] > a[j])
{
int t;
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
fclose(mod);
mod = fopen(argv[2], "wb");
for(int k=0; kfprintf(mod, "%d\r\n", a[k]);
fclose(mod);
mod = fopen(argv[2], "wb");
char part[1000];
for(k=0; k10);
strcat(part, ".part");
fprintf(mod, "%s\r\n", part);
}
fclose(mod);
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");
}