Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从stdin读取文本文件在最后一行停止_C_Stdin_Eof_Feof - Fatal编程技术网

C 从stdin读取文本文件在最后一行停止

C 从stdin读取文本文件在最后一行停止,c,stdin,eof,feof,C,Stdin,Eof,Feof,我编写了一个简短的程序来测试从stdin读取文本文件: int main(){ char c; while(!feof(stdin)){ c = getchar(); //on last iteration, this returns '\n' if(!isspace(c)) //so this is false putchar(c); //remove spaces

我编写了一个简短的程序来测试从
stdin
读取文本文件:

int main(){
    char c;

    while(!feof(stdin)){

        c = getchar();       //on last iteration, this returns '\n'

        if(!isspace(c))      //so this is false
            putchar(c);

        //remove spaces
        while (!feof(stdin) && isspace(c)){    //and this is true
                c = getchar();  //      <-- stops here after last \n
                if(!isspace(c)){
                    ungetc(c, stdin);
                    putchar('\n');
                }
        }
    }
    return 0;
}
最后一行(
joey 7
)以
\n
字符结尾

我的问题是,在它读取并打印最后一行之后,然后循环返回以检查是否有更多的输入,没有更多的字符可读取,它只是在代码块中记录的行处停止


问题:
feof()
返回true的唯一方法是在读取失败后,如下所述:。为什么对
getchar
的最后一次调用没有触发EOF?我如何更好地处理此事件?

您的代码中存在多个问题:

  • 您没有包含
    ,也没有
    ,或者至少您没有发布整个源代码
  • 您可以使用
    feof()
    检查文件的结尾。这几乎从来都不是正确的方法,如中所强调的
  • char
    变量中从流中读取字节。这会阻止对
    EOF
    进行正确的测试,并且还会导致
    isspace(c)
    的未定义行为。将类型更改为
    int
以下是一个改进的版本:

#include <stdio.h>

int main(void) {
    int c;

    while ((c = getchar()) != EOF) {
        if (!isspace(c)) {
            putchar(c);
        } else {
            //remove spaces
            while ((c = getchar()) != EOF && isspace(c)) {
                continue;  // just ignore extra spaces
            }
            putchar('\n');
            if (c == EOF)
                break;
            ungetc(c, stdin);
        }
    }
    return 0;
}

您的代码中存在多个问题:

  • 您没有包含
    ,也没有
    ,或者至少您没有发布整个源代码
  • 您可以使用
    feof()
    检查文件的结尾。这几乎从来都不是正确的方法,如中所强调的
  • char
    变量中从流中读取字节。这会阻止对
    EOF
    进行正确的测试,并且还会导致
    isspace(c)
    的未定义行为。将类型更改为
    int
以下是一个改进的版本:

#include <stdio.h>

int main(void) {
    int c;

    while ((c = getchar()) != EOF) {
        if (!isspace(c)) {
            putchar(c);
        } else {
            //remove spaces
            while ((c = getchar()) != EOF && isspace(c)) {
                continue;  // just ignore extra spaces
            }
            putchar('\n');
            if (c == EOF)
                break;
            ungetc(c, stdin);
        }
    }
    return 0;
}

我不确定。它没有检测到任何失败的读取?请按照链接进行读取。您应该检查
getchar()
(它返回的是
int
,而不是
char
)的返回值,并测试EOF。将
feof()
作为
while()
语句的条件进行测试几乎总是错误的。可以,但是当没有
char
可获取时,对
getchar
的最终调用仍然失败。它就挂在那里。任何使用
而(!feof(fp))
愚蠢的书都应该被烧掉。数百万学生被它误导了。我不确定。它没有检测到任何失败的读取?请按照链接进行读取。您应该检查
getchar()
(它返回的是
int
,而不是
char
)的返回值,并测试EOF。将
feof()
作为
while()
语句的条件进行测试几乎总是错误的。可以,但是当没有
char
可获取时,对
getchar
的最终调用仍然失败。它就挂在那里。任何使用
而(!feof(fp))
愚蠢的书都应该被烧掉。数以百万计的学生被它误导了。我读了这个链接,认为我对这个主题理解得更好。谢谢你。但是,即使合并了建议的更改,执行仍然会在最后一次调用时停止。在11号线。你说它停是什么意思?它至少应该输出
'\n'
好的,我认为这是eclipse调试器的问题。在eclipse中,当调试行到达
while((c=getchar())!=EOF&&isspace(c)时
它只是停止,不再继续。我唯一的选择是按下停止按钮。当我编译它并在终端上运行它时,它工作得很好。对此表示抱歉。这肯定是eclipse问题。我刚刚在cLion中尝试了相同的代码,它也工作得很好。我必须单独调查eclipse问题。谢谢你的帮助。我阅读了链接,认为我更了解这个主题。所以谢谢你。但是即使合并了建议的更改,执行仍然会在最后一次
getchar()时停止
调用。在第11行。它停止是什么意思?它至少应该输出
'\n'
好的,我认为这是eclipse调试器的问题。在eclipse中,当调试行到达
时((c=getchar())!=EOF&&isspace(c)
它只是停止,不再继续。我唯一的选择是按下停止按钮。当我编译它并在终端上运行它时,它工作得很好。对此表示抱歉。这肯定是eclipse问题。我刚刚在cLion中尝试了相同的代码,它也工作得很好。我必须单独调查eclipse问题.谢谢你的帮助。
#include <stdio.h>
#include <ctype.h>

int main(void) {
    int c, last;

    for (last = '\n'; ((c = getchar()) != EOF; last = c) {
        if (!isspace(c)) {
            putchar(c);
        } else
        if (!isspace(last))
            putchar('\n');
        }
    }
    return 0;
}