使用c代码从文件中读取字符

使用c代码从文件中读取字符,c,character-encoding,char,character,C,Character Encoding,Char,Character,我编写了一个C代码来计算文件中的字符、单词和行数。 代码如下 #include "stdio.h" #include "conio.h" #define FILE_NAME "abc.txt" void main() { FILE *fr; int noc = 0; int now = 0; int nol = 0; char ch; printf("hello world\n"); getch(); //clrscr();

我编写了一个C代码来计算文件中的字符、单词和行数。
代码如下

#include "stdio.h"
#include "conio.h"

#define FILE_NAME "abc.txt"

void main()
{
    FILE *fr;
    int noc = 0;
    int now = 0;
    int nol = 0;
    char ch;

    printf("hello world\n");
    getch();

    //clrscr();

    fr = fopen("..\\abc.txt","r");

    if(fr == NULL)
    {
        printf("\n error \n");
        getch();
        return;
    }

    ch = fgetc(fr);
    while(ch != EOF)
    {
        printf("%c", ch);
        noc++;
        if(ch == ' ');
        {
            now++;
        }
        if(ch=='\n')
        {
            nol++;
            now++;
        }
        ch=fgetc(fr);
    }

    fclose(fr);

    printf("\n noc = %d now = %d nol = %d\n", noc, now, nol);
    getch();
}
Hello my friend. 
How are you doing? 
我的文件abc.txt如下

#include "stdio.h"
#include "conio.h"

#define FILE_NAME "abc.txt"

void main()
{
    FILE *fr;
    int noc = 0;
    int now = 0;
    int nol = 0;
    char ch;

    printf("hello world\n");
    getch();

    //clrscr();

    fr = fopen("..\\abc.txt","r");

    if(fr == NULL)
    {
        printf("\n error \n");
        getch();
        return;
    }

    ch = fgetc(fr);
    while(ch != EOF)
    {
        printf("%c", ch);
        noc++;
        if(ch == ' ');
        {
            now++;
        }
        if(ch=='\n')
        {
            nol++;
            now++;
        }
        ch=fgetc(fr);
    }

    fclose(fr);

    printf("\n noc = %d now = %d nol = %d\n", noc, now, nol);
    getch();
}
Hello my friend. 
How are you doing? 
我得到以下输出:

hello world
Hello my friend.
How are you doing?

 noc = 38 now = 40 nol = 2
代码不能正确读取任何字符和行。但是,它将每个字符视为一个单词。我不明白当我数不出上面代码中的单词时,我错在哪里了

请详细解释。

提前谢谢

if(ch==”<代码>如果(ch='')
char ch
永远不会是EOF,因为EOF是一个整数常量。如果要使用
fgetc()
查找EOF,请将
ch
定义为
int
。小心
char
,因为它是由实现定义的,无论它是否有符号。
char ch
永远不会是EOF,因为EOF是一个整数常量。如果要使用
fgetc()
查找EOF,请将
ch
定义为
int
。当心
char
,因为它是由实现定义的,无论它是否有符号。@Jay您的程序很好而且易于练习,但我希望您知道,如果文件中的单词之间使用了多个空格或制表符,它将不会给出正确的输出。是的。。我已经为此添加了以下内容:if((prevch!='&&prevch!='\t'&&prevch!='\n')&((ch='.| ch='\t'.| ch='\n'))@Jay:您可以通过
测试空格(
'\t'
'
)字符是否为空(char
),或者通过
测试不可打印的字符!isprint(char)
,或对于非图形字符,由
!isgraph(char)
一步到位。@jay EOF是对的,而且我认为
如果((prevch!='&&prevch!='\t'&&prevch!='\n')&((ch=''.| ch='\t'.| ch='\n'),
在更容易出错的情况下(如果空间多于2、3或5)会怎样,您应该尝试跳过文件中单词之间可能出现的任何空白字符。有关实现,请参阅文章。感谢各位提供的宝贵意见。真的很感谢你的程序很好,而且练习起来很简单,但是我希望你知道,如果文件中的单词之间使用了多个空格或制表符,它将不会给出正确的输出。是的。。我已经为此添加了以下内容:if((prevch!='&&prevch!='\t'&&prevch!='\n')&((ch='.| ch='\t'.| ch='\n'))@Jay:您可以通过
测试空格(
'\t'
'
)字符是否为空(char
),或者通过
测试不可打印的字符!isprint(char)
,或对于非图形字符,由
!isgraph(char)
一步到位。@jay EOF是对的,而且我认为
如果((prevch!='&&prevch!='\t'&&prevch!='\n')&((ch=''.| ch='\t'.| ch='\n'),
在更容易出错的情况下(如果空间多于2、3或5)会怎样,您应该尝试跳过文件中单词之间可能出现的任何空白字符。有关实现,请参阅文章。感谢各位提供的宝贵意见。我真的很感激你。
if(ch == ' '); <------- see this (remove semicolon)
{
     now++;
}