我正在编写一个分割文件的程序,但是在运行时遇到了一些问题,我不知道如何解决它。
给我一些指导。有时分割为0kb的所有文件有时都有数据。它困惑了我。
#include <stdio.h>
#include <stdlib.h>
void main()
{
char *fn = new char[250];
char *nfn = new char[250];
long int size, n, size_per_pack;
FILE *f, *ft; //file and temp file
printf("enter the file you want to split with full path : ");
scanf("%s", fn);
printf("enter the number of parts you want to split the file : ");
scanf("%ld", &n);
f = fopen(fn, "rb");
if (f == NULL)
{
printf("couldn't open file");
exit(0);
}
fseek(f, 0, 2);
size = ftell(f);
printf("the size of the file in bytes is : %ld\n", size);
size_per_pack = size / n;
rewind(f);
int m = 0;
char *s1 = new char[size_per_pack];
for (int j = 1; j <= n; j++)
{
sprintf(nfn, "%s.%d", fn, j);
ft = fopen(nfn, "wb");
fread(s1, 1, size_per_pack, f);
fwrite(&s1, 1, size_per_pack, ft);
rewind(ft);
int present = ftell(f);
int present2 = ftell(ft);
}
delete[]s1;
delete[]fn;
delete[]nfn;
_fcloseall();
}
答案 0 :(得分:0)
您应该在每次循环迭代后关闭输出文件:
for (int j = 1; j <= n; j++)
{
sprintf(nfn, "%s.%d", fn, j);
ft = fopen(nfn, "wb");
fread(s1, 1, size_per_pack, f);
fwrite(s1, 1, size_per_pack, ft);
fclose(ft);
}