当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Ruby IO.binread用法及代码示例

本文简要介绍ruby语言中 IO.binread 的用法。

用法

binread(name, [length [, offset]]) → string
binread(name, [length [, offset]]) → string

打开文件,可选择查找给定的 offset ,然后返回 length 字节(默认为文件的其余部分)。 binread 确保文件在返回之前关闭。打开模式将是 "rb:ASCII-8BIT"

如果 name 以管道字符 ("|" ) 开头,并且接收者是 IO 类,则以与 Kernel#open 相同的方式创建子进程,并返回其输出。考虑使用 File.binread 来禁用子进程调用的行为。

File.binread("testfile")           #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
File.binread("testfile", 20)       #=> "This is line one\nThi"
File.binread("testfile", 20, 10)   #=> "ne one\nThis is line "
IO.binread("| cat testfile")       #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"

有关name 和open_args 的详细信息,另请参见 IO.read

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 IO.binread。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。