7.2 打开文件

本章介绍的过程用于打开文件端口。用于打开其他类型的端口(比如,字符串端口,定制端口)的过程在接下来的章节里介绍。

每一个打开文件的操作接受一个 path 参数,用于指定要打开的文件。它必须是一个字符串或其它特定于具体实现的值。

一些打开文件的过程接受可选的optionsb-mode, ?transcoder参数,options必须是一个由下述file-options条目中合法的符号构成的枚举集合,它的默认值是 (file-options)表达式的值。b-mode必须是合法的缓冲模式,默认为 block?transcoder必须是一个代码转换器,或者是 #f。如果它是一个代码转换器,打开操作返回一个对底层的二进制文件进行解码后的端口,如果它是 #f, 则返回一个二进制端口。

syntax: (file-options symbol ...)
returns: 一个 file-options 枚举集合
libraries: (rnrs io ports), (rnrs)

文件选项枚举集合可以传递给文件打开过程以控制打开操作的方方面面。有3个标准的文件选项:no-create, no-fail, no-truncate. 只影响创建输出端口(包括输入/输出双向端口)的文件打开操作。

只使用默认的文件选项(file-options), 当程序试图挨打一个输出文件时,如果文件已经存在,会抛出i/o-file-already-exists异常, 如果文件不存在则新建一个文件。

如果文件选项里包含了no-fail选项,当文件存在时不抛出异常,然而已存在的文件会被清空。

如果包含了no-create选项,当文件不存在时并不新建文件,而是抛出i/o-file-does-not-exists异常。no-create隐含了no-fail选项。

no-truncate仅仅与no-fail相关,只有在显式地或隐含地使用了no-fail选项的时候才使用。包含了no-truncate选项后,已存在的文件不会被清空,但是文件指针依旧会设置到文件开头。

可以假设默认文件选项包含了虚构的create, fail-if-exists, truncate; 当使用了 no-create后,会删除掉create, no-fail会删除fail-if-exists, no-truncate会删除truncate

具体的 Scheme 实现可能会支持附加的选项符号。例如,Chez Scheme 支持附加的选项以控制文件是否压缩,是否加锁以独占访问,以及新建文件的权限。[9]

syntax: (buffer-mode symbol)
returns: symbol
libraries: (rnrs io ports), (rnrs)

symbol必须是block, line, none三者之一。表达式(buffer-mode symbol)(quote symbol)等价,除了前者会在宏展开期检查符号的有效性。buffer-mode宏提供了有用的文档。

(buffer-mode block) => block
(buffer-mode cushion) => syntax violation

syntax: (buffer-mode? obj)
returns: #t if obj is a valid buffer mode, #f otherwise
libraries: (rnrs io ports), (rnrs)

(buffer-mode? 'block) => #t
(buffer-mode? 'line) => #t
(buffer-mode? 'none) => #t
(buffer-mode? 'something-else) => #f

procedure: (open-file-input-port path)
procedure: (open-file-input-port path options)
procedure: (open-file-input-port path options b-mode)
procedure: (open-file-input-port path options b-mode ?transcoder)
returns: a new input port for the named file
libraries: (rnrs io ports), (rnrs)

如果指定了?transcoder参数,并且不是#f,那么它必须是一个代码转换器,open-file-input-port过程会返回一个文件端口,否则,该过程返回一个二进制端口。

procedure: (open-file-output-port path)
procedure: (open-file-output-port path options)
procedure: (open-file-output-port path options b-mode)
procedure: (open-file-output-port path options b-mode ?transcoder)
returns: a new output port for the named file
libraries: (rnrs io ports), (rnrs)

If ?transcoder is present and not #f, it must be a transcoder, and this procedure returns a textual output port whose transcoder is ?transcoder. Otherwise, this procedure returns a binary output port. See the lead-in to this section for a description of the constraints on and effects of the other arguments.

procedure: (open-file-input/output-port path)
procedure: (open-file-input/output-port path options)
procedure: (open-file-input/output-port path options b-mode)
procedure: (open-file-input/output-port path options b-mode ?transcoder)
returns: a new input/output port for the named file
libraries: (rnrs io ports), (rnrs)

If ?transcoder is present and not #f, it must be a transcoder, and this procedure returns a textual input/output port whose transcoder is ?transcoder. Otherwise, this procedure returns a binary input/output port. See the lead-in to this section for a description of the constraints on and effects of the other arguments.

results matching ""

    No results matching ""