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
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.
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:
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.
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.
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.
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.
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.