\\在C 377字符 [英] \377 character in c

查看:335
本文介绍了\\在C 377字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取C的文件。
我有一个txt文件,它有内容:

file_one.txt file_two.txt file_three.txt file_four.txt

当我尝试读取的fopen这个文件我得到如下的输出:

file_one.txt file_two.txt file_three.txt file_four.txt \\ 377

什么\\ 377是什么意思?这里是我的code。

 的#include<&stdio.h中GT;    #包括LT&;&stdlib.h中GT;    INT主(INT ARGC,为const char * argv的[]){        FILE *文件清单;        焦炭CH;        文件清单= FOPEN(文件路径,RT);        而(!的feof(文件清单)){
            CH = GETC(文件清单);
            的printf(%C,CH);
        }        FCLOSE(文件清单);        返回0;
    }


解决方案

GETC()函数返回类型的结果 INT ,而不是类型的字符。你的字符CH; INT CH;

为什么它返回一个 INT ?因为它返回的值的或者的只是读取的字符(作为 unsigned char型转换为 INT )的的特殊值 EOF (通常为-1),表明无论是输入错误或档案结尾的条件。

不要使用的feof()函数来检测输入的结束。它返回true唯一的之后的你已经用完了投入。你上次调用 GETC()将返回 EOF ,当其存储到字符对象转换为(炭)-1 ,这通常是'\\ 377'

另一个问题是,的feof()将的从不的返回真值如果有输入错误;在这种情况下, FERROR()将返回true。使用的feof()和/或 FERROR()之后 GETC()收益 EOF ,告诉的为什么的它返回 EOF

,直到达到它的结束从文件中读取:

  INT CH;
而((CH = GETC(文件清单))!= EOF){
    / * CH包含的最后一个字符读取;做你喜欢它* /
}

阅读推荐:第12条的 comp.lang.c常见问题解答

i am trying to read a file in c. i have a .txt file and it has that content:

file_one.txt file_two.txt file_three.txt file_four.txt

when i try to read this file with fopen i get this output:

file_one.txt file_two.txt file_three.txt file_four.txt\377

what does \377 mean? Here's my code.

    #include <stdio.h>

    #include <stdlib.h>

    int main(int argc, const char * argv[]){

        FILE *filelist;

        char ch;

        filelist=fopen("file-path", "rt");

        while (!feof(filelist)) {
            ch = getc(filelist);
            printf("%c",ch);
        }

        fclose(filelist);

        return 0;
    }

解决方案

The getc() function returns a result of type int, not of type char. Your char ch; should be int ch;.

Why does it return an int? Because the value it returns is either the character it just read (as an unsigned char converted to int) or the special value EOF (typically -1) to indicate either an input error or an end-of-file condition.

Don't use the feof() function to detect the end of input. It returns true only after you've run out of input. Your last call to getc() is returning EOF, which when stored into a char object is converted to (char)-1, which is typically '\377'.

Another problem is that feof() will never return a true value if there was an input error; in that case, ferror() will return true. Use feof() and/or ferror() after getc() returns EOF, to tell why it returned EOF.

To read from a file until you reach the end of it:

int ch;
while ((ch = getc(filelist)) != EOF) {
    /* ch contains the last character read; do what you like with it */
}

Suggested reading: Section 12 of the comp.lang.c FAQ.

这篇关于\\在C 377字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆