文本文件中的“a”号 [英] No. of 'a' in a text file
问题描述
/ *我编写了以下程序来计算否。文件中的''a''
c:/1.txt但它未能给出适当的结果。有什么问题
呢? * /
#include" stdio.h"
int main(无效)
{
FILE * f;
char ch;
long int a = 0;
f = fopen(" c:/1.txt" ,r);
while(ch = getc(f)!= EOF)
{
switch(ch)
{
case''a'':a ++; break;
}
}
printf("''a''=%d \ n",a);
fclose(f);
返回0;
}
/* I wrote the following program to calculate no. of ''a'' in the file
c:/1.txt but it fails to give appropriate result. What is wrong with
it? */
#include"stdio.h"
int main(void)
{
FILE *f;
char ch;
long int a=0;
f=fopen("c:/1.txt","r");
while(ch=getc(f)!=EOF)
{
switch(ch)
{
case ''a'': a++;break;
}
}
printf("No. of ''a'' = %d\n",a);
fclose(f);
return 0;
}
推荐答案
ume
h说:
h said:
/ *我编写了以下程序来计算否。文件中的''a''
c:/1.txt但它未能给出适当的结果。有什么问题
呢? * /
#include" stdio.h"
int main(无效)
{
FILE * f;
char ch;
long int a = 0;
f = fopen(" c:/1.txt" ," R");
/* I wrote the following program to calculate no. of ''a'' in the file
c:/1.txt but it fails to give appropriate result. What is wrong with
it? */
#include"stdio.h"
int main(void)
{
FILE *f;
char ch;
long int a=0;
f=fopen("c:/1.txt","r");
如果fopen失败会怎么样?
What happens if the fopen fails?
while(ch = getc(f)!= EOF )
while(ch=getc(f)!=EOF)
你确定要说这个吗?考虑=和!=
的优先级并检查getc的返回类型。
Are you sure you meant to say this? Consider the precedences of = and !=
and check the return type of getc.
{
switch (ch)
{
case''a'':a ++; break;
{
switch(ch)
{
case ''a'': a++;break;
你真的打算在这一点上突破你的循环吗?
Do you really mean to break out of your loop at this point?
}
}
printf("'a''=%d \ n',')的编号;
}
}
printf("No. of ''a'' = %d\n",a);
您是否阅读过printf的文档?
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
Did you read the documentation for printf?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
ume
这篇关于文本文件中的“a”号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!