【发布时间】:2015-04-17 15:24:37
【问题描述】:
我正在编写一个拆分文件的程序,但运行时遇到了一些问题,我不知道如何解决。
给我一些指导。有时拆分为 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();
}
【问题讨论】:
-
那是纯C++,不是C!
-
你使用有什么原因吗?
char *fn = new char[250];而不是char fn[250];或std::array<char, 250> fn;,或者最好的:std::string fn;? -
一个更有趣的问题是你为什么要用 C++ 编写 C 代码?
-
我只用动态数组,其余都是C
-
从什么时候开始fread和fwrite纯C++?他“新建”了一个数组,几乎所有这些都不是纯 C 代码。