Ruby cross-platform way to write EOF character

Is there a platform-independent way to write an EOF character to a string in Ruby. On * nix, I think the character is ^D , but on Windows it is ^Z , so I ask.

+6
string ruby cross-platform eof
source share
2 answers

EOF is not a symbol, it is a state. Terminals use control characters to represent this state (Cd). There is no such thing, it is "reading the EOF character" and the same thing for writing. If you are writing a file, just close it when you're done. See this mailing list :

It looks like you think of EOF as an in-band but special character value that marks the end of the file. It is better to think of it as an out-of-band sentinel value. In C, EOF is usually -1, and the corresponding API sets the integer value, so EOF will never be confused with the actual value inside the strip.

Here are some more proofs (do it on Unix):

 $ cat > file hello^V^Dworld ^D $ cat file helloworld 

Entering ^ V ^ D inserts the control character -D literally into the file. After entering the world and entering, ^ D closes the pipe. The file ends with a length of 12 bytes of 10 letters, two more for ^ D and a new line. The final ^ D does not get into the file. It is simply used by the terminal / sheath to close the pipe.

+17
source share

In general, there is no EOF symbol. That is, there is no cross-platform solution to this and even on specific platforms, the processing of such a character is purely hereditary and inconsistent. You complete the file by closing it.

However, to be pedantic, some operating systems support the literal end of a file character when reading files in certain modes. For example, if you are running Windows and using the C stdio API to read a file in text mode, then the literal control-Z (character code 26) will signal the completion of the file for stdio. This is a hold on MS-DOS that it has as a hold on CP / M. If you use stdio and read the file in binary mode, then control-Z will not complete the file.

However, you should only think of it as "know, do not use." You will want to know about this if you ever see overloaded I / O on Windows, but using it is crazy.

+5
source share

All Articles