主题:大家看一下我这个程序,能运行但出错了,但出错很神奇,谁知道怎么了?
该程序的意思是计算一个已知文本的单词的个数:
#include<stdio.h>
#include<stdlib.h>
void main()
{
char ch[500];
int n=0,i=0;
FILE *fp;
if((fp=fopen("2.txt","r"))==NULL)
{
printf("Can't open file\n");
exit(0);
}
while(!feof(fp))
{
fgets(ch,500,fp);
for(;ch[i]!='\0';)
{
while(ch[i]==' ')
{putchar(ch[i]);i++;}
while(ch[i]!=' ')
{putchar(ch[i]);i++;}
n++;
}
}
fclose(fp);
printf("%d\n",n);
}
#include<stdio.h>
#include<stdlib.h>
void main()
{
char ch[500];
int n=0,i=0;
FILE *fp;
if((fp=fopen("2.txt","r"))==NULL)
{
printf("Can't open file\n");
exit(0);
}
while(!feof(fp))
{
fgets(ch,500,fp);
for(;ch[i]!='\0';)
{
while(ch[i]==' ')
{putchar(ch[i]);i++;}
while(ch[i]!=' ')
{putchar(ch[i]);i++;}
n++;
}
}
fclose(fp);
printf("%d\n",n);
}