ProbIem with EOF in C

后端 未结 7 747
既然无缘
既然无缘 2021-01-01 03:48

I\'m writing a program which is supposed to read two strings that can contain line breaks and various other characters. Therefore, I\'m using EOF (Ctrl-Z or Ctrl-D) to end t

7条回答
  •  一生所求
    2021-01-01 04:33

    What you are trying is fundamentally impossible with EOF.

    Although it behaves like one in some ways, EOF is not a character in the stream but an environment-defined macro representing the end of the stream. I haven't seen your code, but I gather you're doing is something like this:

    while ((c=getchar()) != EOF) {
        // do something
    }
    while ((c=getchar()) != EOF) {
        // do something else
    }
    

    When you type the EOF character the first time, to end the first string, the stream is irrevocably closed. That is, the status of the stream is that it is closed.

    Thus, the contents of the second while loop are never run.

提交回复
热议问题