當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。