这很棘手。不幸的是,不直接支持搜索。此外,文件位置仅对于二进制文件是透明的,即您使用BinIO
结构[1]。对于该结构体,对应的类型BinIO.StreamIO.pos
被定义为Position.int
,这是某种整数类型。
然而,在支持标准的完整 I/O 堆栈的 SML 系统中,您应该能够使用较低的 I/O 层综合以下查找函数:
(* seekIn : BinIO.instream * Position.int -> unit *)
fun seekIn(instream, pos) =
case BinIO.StreamIO.getReader(BinIO.getInstream instream) of
(reader as BinPrimIO.RD{setPos = SOME f, ...}, _) =>
( f pos;
BinIO.setInstream(instream,
BinIO.StreamIO.mkInstream(reader, Word8Vector.fromList[]))
)
| (BinPrimIO.RD{name, ...}, _) =>
raise IO.Io{
name = name,
function = "seekIn",
cause = IO.RandomAccessNotSupported
}
像这样使用它:
val file = BinIO.openIn "filename"
val _ = seekIn(file, 200)
val bin = BinIO.inputN(file, 1000)
如果需要从 Word8Vector 转换为字符串:
val s = Byte.bytesToString bin
您也可以对输出流执行相同的操作。
[1] http://standardml.org/Basis/bin-io.html#BIN_IO:SIG:SPEC http://standardml.org/Basis/bin-io.html#BIN_IO:SIG:SPEC