是否在返回EOF后定义了getc()?

orl*_*rlp 4 c io

getc();在C练习中使用,在回顾程序后我发现了一些奇怪的东西.我假设在命令行参数上给出的文件至少包含一个字节.(它getc();在没有检查的情况下连续调用两次EOF.在空文件上尝试之后它仍然可以顺利运行.我的问题是:文件指针的行为getc();是否已经耗尽(已达到EOF并且没有重新启动)未定义或将它总是继续返回EOF?

我想我可以将这个问题扩展到C STL中的所有I/O函数,请在你的答案中澄清这一点.

这是该程序的代码.该程序应该从所有注释中剥离C/C++源文件(并且它完美地工作).

#include <stdio.h>

int main(int argc, char *argv[]) {
    int state = 0; // state: 0 = normal, 1 = in string, 2 = in comment, 3 = in block comment
    int ignchar = 0; // number of characters to ignore
    int cur, next; // current character and next one
    FILE *fp; // input file

    if (argc == 1) {
        fprintf(stderr, "Usage: %s file.c\n", argv[0]);
        return 1;
    }

    if ((fp = fopen(argv[1], "r")) == NULL) {
        fprintf(stderr, "Error opening file.\n");
        return 2;
    }

    cur = getc(fp); // initialise cur, assumes that the file contains at least one byte
    while ((next = getc(fp)) != EOF) {
        switch (next) {
            case '/':
                if (!state && cur == '/') {
                    state = 2; // start of comment
                    ignchar = 2; // don't print this nor next char (//)
                } else if (state == 3 && cur == '*') {
                    state = 0; // end of block comment
                    ignchar = 2; // don't print this nor next char (*/)
                }
                break;
            case '*':
                if (!state && cur == '/') {
                    state = 3; // start of block comment
                    ignchar = 2; // don't print this nor next char (/*)
                }
                break;
            case '\n':
                if (state == 2) {
                    state = 0;
                    ignchar = 1; // don't print the current char (cur is still in comment)
                }
                break;
            case '"':
                if (state == 0) {
                    state = 1;
                } else if (state == 1) {
                    state = 0;
                }
        }

        if (state <= 1 && !ignchar) putchar(cur);
        if (ignchar) ignchar--;
        cur = next;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

R..*_*R.. 6

Stdio文件保留一个"eof"标志,该标志在第一次到达文件结束时设置,并且只能通过调用clearerr或执行成功fseek或重置来重置rewind.因此,一旦getc返回EOF一次,它将继续返回EOF,即使新数据可用,除非您使用上述方法之一清除eof标志.

一些不符合的实现可以立即使新数据可用.此行为是有害的,可能会破坏符合要求的应用程序.