Ruby以跨平台的方式编写EOF符号

时间:2021-07-21 12:21:12

Is there a platform-independent way of writing the EOF symbol to a string in Ruby. In *nix I believe the symbol is ^D, but in Windows is ^Z, that's why I ask.

是否有一种独立于平台的方式将EOF符号写入Ruby中的字符串。在* nix我相信符号是^ D,但在Windows中是^ Z,这就是我问的原因。

2 个解决方案

#1


16  

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

EOF不是一个角色,它是一个国家。终端使用控制字符来表示此状态(C-d)。没有这样的事情是“读一个EOF角色”和写一个相同的东西。如果您要写入文件,请在完成后关闭它。查看此邮件列表帖子:

It sounds like you are thinking of EOF as an in-band but special character value that marks the end of file. It is better to think of it as an out-of-band sentinel value. In C, EOF is usually -1 and the associated API specifies integer return values so that EOF is guaranteed to never be confused with a valid in-band value.

听起来你正在考虑将EOF作为带内但特殊的字符值来标记文件的结尾。最好将其视为带外哨兵价值。在C中,EOF通常为-1,关联的API指定整数返回值,以保证EOF永远不会与有效的带内值混淆。

Here's some more proof (do this on Unix):

这里有一些证据(在Unix上这样做):

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

Typing ^V^D inserts a control-D character literally into the file. After typing world and enter, the ^D closes the pipe. The file ends up being 12 bytes long 10 letters, two more for the ^D and the newline. The final ^D does not end up in the file. It's just used by the terminal/shell to close the pipe.

键入^ V ^ D将控制字符字面插入文件中。键入world并输入后,^ D关闭管道。该文件最终为12个字节长10个字母,另外两个用于^ D和换行符。最终的^ D不会在文件中结束。它只是被终端/ shell用来关闭管道。

#2


5  

In general there is no EOF character. That is, there's no cross-platform solution to this and even on specific platforms the handling of such a character is purely legacy and inconsistent. You end a file by closing it.

通常没有EOF字符。也就是说,没有跨平台解决方案,即使在特定平台上,这样一个角色的处理纯粹是遗留的和不一致的。您通过关闭它来结束文件。

However, to be pedantic, certain operating systems when reading files in certain modes do support a literal end of file character. For example, if you're running under Windows and use the C stdio API to read a file in text mode then a literal control-Z (character code 26) will signal end of file to stdio. This is a holdover from MS-DOS which it has as a holdover from CP/M. If you use stdio and read the file in binary mode then the control-Z will not end the file.

但是,为了迂腐,某些操作系统在某些模式下读取文件时确实支持文件结尾字符。例如,如果您在Windows下运行并使用C stdio API以文本模式读取文件,则文字控件-Z(字符代码26)将向文件结束信号发送到stdio。这是来自MS-DOS的保留,它作为CP / M的保留。如果您使用stdio并以二进制模式读取文件,则control-Z将不会结束该文件。

Nevertheless, you should only think of it as "know, don't use" feature. You'll want to know about it if you ever see trucated input/output on Windows, but using it is madness.

不过,您应该只将其视为“知道,不使用”功能。如果你在Windows上看到套接字输入/输出,你会想知道它,但使用它是疯狂的。

#1


16  

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

EOF不是一个角色,它是一个国家。终端使用控制字符来表示此状态(C-d)。没有这样的事情是“读一个EOF角色”和写一个相同的东西。如果您要写入文件,请在完成后关闭它。查看此邮件列表帖子:

It sounds like you are thinking of EOF as an in-band but special character value that marks the end of file. It is better to think of it as an out-of-band sentinel value. In C, EOF is usually -1 and the associated API specifies integer return values so that EOF is guaranteed to never be confused with a valid in-band value.

听起来你正在考虑将EOF作为带内但特殊的字符值来标记文件的结尾。最好将其视为带外哨兵价值。在C中,EOF通常为-1,关联的API指定整数返回值,以保证EOF永远不会与有效的带内值混淆。

Here's some more proof (do this on Unix):

这里有一些证据(在Unix上这样做):

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

Typing ^V^D inserts a control-D character literally into the file. After typing world and enter, the ^D closes the pipe. The file ends up being 12 bytes long 10 letters, two more for the ^D and the newline. The final ^D does not end up in the file. It's just used by the terminal/shell to close the pipe.

键入^ V ^ D将控制字符字面插入文件中。键入world并输入后,^ D关闭管道。该文件最终为12个字节长10个字母,另外两个用于^ D和换行符。最终的^ D不会在文件中结束。它只是被终端/ shell用来关闭管道。

#2


5  

In general there is no EOF character. That is, there's no cross-platform solution to this and even on specific platforms the handling of such a character is purely legacy and inconsistent. You end a file by closing it.

通常没有EOF字符。也就是说,没有跨平台解决方案,即使在特定平台上,这样一个角色的处理纯粹是遗留的和不一致的。您通过关闭它来结束文件。

However, to be pedantic, certain operating systems when reading files in certain modes do support a literal end of file character. For example, if you're running under Windows and use the C stdio API to read a file in text mode then a literal control-Z (character code 26) will signal end of file to stdio. This is a holdover from MS-DOS which it has as a holdover from CP/M. If you use stdio and read the file in binary mode then the control-Z will not end the file.

但是,为了迂腐,某些操作系统在某些模式下读取文件时确实支持文件结尾字符。例如,如果您在Windows下运行并使用C stdio API以文本模式读取文件,则文字控件-Z(字符代码26)将向文件结束信号发送到stdio。这是来自MS-DOS的保留,它作为CP / M的保留。如果您使用stdio并以二进制模式读取文件,则control-Z将不会结束该文件。

Nevertheless, you should only think of it as "know, don't use" feature. You'll want to know about it if you ever see trucated input/output on Windows, but using it is madness.

不过,您应该只将其视为“知道,不使用”功能。如果你在Windows上看到套接字输入/输出,你会想知道它,但使用它是疯狂的。