标题:输不出答案,但找不到哪有错,各位帮忙看看
只看楼主
clara
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-5-27
结帖率:100%
已结贴  问题点数:5 回复次数:1 
输不出答案,但找不到哪有错,各位帮忙看看
#include <stdio.h>
void main()
{
    //file *fpt;
    FILE *fpt;
    int count=0,space=1;
    char ch,filename[20];
    printf("Input the filename:");
    //getchar(filename);
      gets(filename);
    if((fpt=fopen(filename,"r"))=NULL)
    {
        printf("Can't open file %s\n",filename);
        return;
    }
    //while((ch=fputc(fpt))==EOF)
    while((ch=fgetc(fpt))!=EOF)
    switch(ch)
    {
    case ' ':
    case '\t':
    //case '\n': space=0;
    case '\n':space=1;
        break;
    default: if(space)
             {
                 space=0;
                 count++;
             }
    }
    //close(fpt);
    fclose(fpt);
    printf("File '%s' contains %d words.\n",filename,count);
}
搜索更多相关主题的帖子: count return include 看看 
2013-05-31 18:21
YJ_Hao
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:215
专家分:609
注 册:2013-3-22
得分:5 
#include <stdio.h>
void main()
{
    //file *fpt;
    FILE *fpt;
    int count=0,space=1;
    char ch,filename[20];
    printf("Input the filename:");
    //getchar(filename);
      gets(filename);
    if((fpt=fopen(filename,"r"))=NULL)         <-------------------把这里的=改成==就OK了!
    {
        printf("Can't open file %s\n",filename);
        return;
    }
    //while((ch=fputc(fpt))==EOF)
    while((ch=fgetc(fpt))!=EOF)
    switch(ch)
    {
    case ' ':
    case '\t':
    //case '\n': space=0;
    case '\n':space=1;
        break;
    default: if(space)
             {
                 space=0;
                 count++;
             }
    }
    //close(fpt);
    fclose(fpt);
    printf("File '%s' contains %d words.\n",filename,count);
}
2013-05-31 19:35



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-412683-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028213 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved