|
小弟想将一篇英语文章放到txt文本里,
然后利用文本复制到文本中的过程,统计单词数
程序如下:
#include<stdio.h>
#include<string.h>
void main()
{
FILE *in, *out;
char infile[20],outfile[20],ch,string[100],c;
int num = 0, word = 0, i = 0;
printf("Enter the infile name:\n");
scanf("%s",infile);
printf("%s\n",infile);
printf("Enter the outfile name:\n");
scanf("%s",outfile);
printf("%s\n",outfile);
if ((in = fopen(infile,"rb"))==NULL)
{
printf("cannot open infile\n");
exit(-1);
}
if ((out=fopen(outfile,"wb"))==NULL)
{
printf("cannot open outfile\n");
exit(0);
}
for(i = 0; !feof(in); i++)
{
ch = fgetc(in);
printf("%c",ch);
fputc(ch,out);
string = ch;
printf("%c\n",string);
}
for (i = 0;(c = string) != '\0'; i++)
{
if( ch == ' ' && ch == '\n' )
word = 0;
else if(word == 0)
{
word = 1;
num++;
}
}
printf("There are %d words in the %s\n",num,infile);
fclose(in);
fclose(out);
}
但输入的结果是:
Enter the infile name:
C\1.txt
C\1.txt
Enter the outfile name:
C\2.txt
C\2.txt
ii
aa
mm
aa
cc
hh
ii
nn
ee
ss
ee
!!
There are 1 words in the 1.txt
其中用了几个printf来检查程序
发现编译器好像不经过:
for (i = 0;(c = string) != '\0'; i++)
{
if( ch == ' ' && ch == '\n' )
word = 0;
else if(word == 0)
{
word = 1;
num++;
}
}
为什么呢??
向各位高手请教了!
要怎样修改才行?
|