读书人

小疑点

发布时间: 2012-03-18 13:55:39 作者: rapoo

在线等,小问题。

C/C++ code
#include    <stdio.h>/**2、    创建一个文本文件“stringcount.txt”,在其中输入一段英文:I’d like you to come immediately when I call you.写程序统计文本中字符串的个数。*/int countStr(char *ch);static int count = 0;int main(){    FILE *pFile;    char ch;    if((pFile = fopen("D:\\stringcount.txt", "rt")) == NULL)    {        printf("文件不存在");        getch();        exit(-1);    }    while ((ch = getch(pFile)) != EOF)    {        if (ch == '\t')            count++;    }    fclose(pFile);                             // 关闭文件    printf("单词数位:");    printf("%d", count);    return 0;}


编译没问题, 运行后出来命令行,什么都不显示。
我在D盘下建立了,stringcount.txt,求高手解答。

[解决办法]
C/C++ code
while ((ch = fgetc(pFile)) != EOF)
[解决办法]
C/C++ code
#include    <stdio.h>#include <conio.h>#include <stdlib.h>/**2、    创建一个文本文件“stringcount.txt”,在其中输入一段英文:I’d like you to come immediately when I call you.写程序统计文本中字符串的个数。*/int countStr(char *ch);static int count = 0;int main(){    FILE *pFile;    int ch; //char 改为 int    if((pFile = fopen("D:\\stringcount.txt", "rt")) == NULL)    {        printf("文件不存在");        getch();        exit(-1);    }    while ((ch = fgetc(pFile)) != EOF) //改为 fgetc    {         if (ch == '\t')            count++;    }    fclose(pFile);                             // 关闭文件    printf("单词数位:");    printf("%d", count);    return 0;}
[解决办法]
while ((ch = getch(pFile)) != EOF)
这里用法有问题吧.

读书人网 >C语言

热点推荐