各位高手:我写了一个将一个上万行的文本文件,分割成四百行的小文件。程序应该简单,只是需要建立若干个文件,然后将内容写进去。但是在执行时,报"open cut file fail!"错,请各位高手指点错在哪里。非常感谢!
#include <stdio.h>
#include <string.h>
#include <process.h>
void main()
{
FILE *outfp;
FILE *cutfp;
int i;
char postfix = a;
char filename[20] = "gzdest_";
char buf[512]="0";
outfp = fopen("gzdest.txt", "r");
if (outfp == NULL)
{
printf("open gzdest.txt file fail!");
exit(0);
}
//将文件分成小块
while (!feof(outfp))
{
strcat(filename,&postfix);
strcat(filename,".txt");
cutfp = fopen(filename, "w");
if (cutfp == NULL)
{
printf("open cut file fail!");
exit(0);
}
for (i =0; i < 400; i++)
{
fgets(buf,sizeof(buf),outfp);
fputs(buf,cutfp);
}
if (i == 400)
{
i= 0;
postfix++;
fclose(cutfp);
}
}
fclose(outfp);
fclose(cutfp);
}
呵呵
strcat(filename,&postfix);
strcat(filename,".txt");
好象有好多问题....
postfix是char, &postfix是地址,可能产生不存在的文件名....
按你的意思, 第一次应该产生gzdest_a.txt,可是第二次就会是gzdest_a.txtb.txt;....
另外a,b,c....很容易就到达不可打印字符.
可以定文件名为:
int fileno=0;
while (!feof(outfp))
{
sprintf(filename, "gzdest_%d.txt", fileno);
...
}
另外重新开始读文件的时候不设置偏移量,会每个文件的内容都一样...
如果这种方式的话,可能要读去N个字节,这样好计算偏移量...