ProbIem with EOF in C

后端 未结 7 742
既然无缘
既然无缘 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:11

    I run the code on my linux box, here is the result:

    Enter a: qwer
    asdf<Ctrl-D><Ctrl-D>
    Enter b: 123
    456<Ctrl-D><Ctrl-D>
    
    Results:
    a: qwer
    asdf
    b: 123
    456
    

    Two Ctrl-D was needed because the terminal input buffer was not empty.

    0 讨论(0)
  • 2021-01-01 04:16

    EOF isn't a character - it's a special value that the input functions return to indicate a condition, that the "end of file" on that input stream has been reached. As Martin v. Löwis says, once that "end of file" condition occurs, it means that no more input will be available on that stream.

    The confusion arises because:

    • Many terminal types recognize a special keystroke to signal "end of file" when the "file" is an interactive terminal (eg. Ctrl-Z or Ctrl-D); and
    • The EOF value is one of the values that can be returned by the getchar() family of functions.

    You will need to use an actual character value to separate the inputs - the ASCII nul character '\0' might be a good choice, if that can't appear as a valid value within the inputs themselves.

    0 讨论(0)
  • 2021-01-01 04:23

    How do you enter null in the program?

    You can implement the -print0 function using:

    putchar(0);
    

    This will print an ASCII nul character '\0' to sdtout.

    0 讨论(0)
  • 2021-01-01 04:24

    After you received an EOF from the terminal, you will not receive any additional data. There is no way of un-EOF-ing the input - the end of the file is, well, the end.

    So you should define that each variable is input on a separate line, and have users press enter instead of EOF. You still need to check whether you have received eof, because that means that the user actually typed EOF, and you won't see anything else - in this case, you need to break out of the loop and print an error message.

    0 讨论(0)
  • 2021-01-01 04:25

    You could use the null character ('\0') to separate the variables. Various UNIX tools (e.g. find) are capable of separating their output items in this way, which would suggest that it's a fairly standard method.

    Another advantage of this is that you can read the stream into a single buffer and then create an array of char*s to point to the individual strings, and each string will be correctly '\0'-terminated without you having to change anything in the buffer manually. This means less memory allocation overhead, which may make your program run noticeably faster depending on how many variables you're reading. Of course, this is only necessary if you need to hold all the variables in memory at the same time — if you're dealing with them one-at-a-time, you don't get this particular advantage.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题