怎样用C语言将文本分成等字数的段?

来源:百度知道 编辑:UC知道 时间:2024/05/02 20:06:53
有一个1.txt的文本,是一篇文章,共有大约1000个汉字,未分段。我想利用C语言将其分成200字每段的几段汉字。处理完的格式:1.txt的上面部分显示原来文章,下面显示经过处理的几段每段200个汉字的文章。
最好能给我代码,谢谢了!

/*
本程序由Turbo C2.0编译通过。文本文件1.txt请放在Turbo C所在目录下。则输出文件1.txt也在Turbo C所在目录下。
*/

#include "stdio.h"
main()
{
FILE *fp,*temp;
char ch;
int i=0;
clrscr();
if(((fp=fopen("1.txt","r+"))&&(temp=fopen("1.tmp","w+")))==NULL)
{
printf("Can't open file\n");
printf("Press any key to exit...");
getch();
exit(0);
}
printf("\nPlease wait...");
while(!feof(fp))
{
fputc(fgetc(fp),temp);
i++;
if(i%200==0) fputc('\n',temp);
}
rewind(temp);
fputc('\n',fp);
fputc('\n',fp);
while(!feof(temp)) fputc(fgetc(temp),fp);
fopen("1.tmp","w");
fclose(fp);
fclose(temp);
printf("OK!\n");
printf("Press any key to exit...");
getch();
printf("