设计一个C程序,要求能统计出用户文本文件中所有单词的个数,并存入结果文件.高分,急求!!

来源:百度知道 编辑:UC知道 时间:2024/09/22 09:22:57

楼上的有bug, 如果多个空格在一起会算成是多个单词.
以下是改良版:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int readDat(char filename[])
{
int n = 0,word = 0;
char c;
FILE *fp;
if((fp=fopen(filename,"r"))==NULL)
{
printf("文件打开错误!\n");
exit(0);
}
printf("文件打开成功!\n");
while(!feof(fp))
{
c = fgetc(fp);
if(c == ' ' || c == '\n')
word = 0;
else
if(word == 0 && !feof(fp))
{
word = 1;
n++;
}
}
fclose(fp);
return n;
}

void writeDat(int i)
{
FILE *fp;
if((fp=fopen("result.txt","w"))==NULL)
{
printf("文件打开错误!\n");
exit(0);
}
fprintf(fp,"文件中的单词数为%d个",i);
printf("结果保存在result.txt当中\n",i);
//prin