| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The keyword arguments specify precise behavior on the exceptional case.
:if-exists
open-output-file, and
specifies the action when the filename already exists.
One of the following value can be given.
:supersede
:append
:error
#f
#f.
:if-does-not-exist
:error
open-input-file.
:create
open-output-file.
The check of file existence and creation is done atomically; you can
exclusively create the file by specifying :error or #f to
if-exists, along this option.
You can't specify this value for open-input-file.
#f
#f.
:buffering
port-buffering.
(See section 6.18.2 Common port operations).
:full
:none
:line
:modest
:full, except that read-block may return less data
than requested if the requested amount of data is not immediately available.
(In the :full mode, read-block waits the entire data to be
read). This is suitable for the port connected to a pipe or network.
:element-type
:character
:binary
By combination of if-exists and if-does-not-exist flags, you can implement various actions:
(open-output-file "foo" :if-exists :error)
=> ;opens "foo" exclusively, or error
(open-output-file "foo" :if-exists #f)
=> ;opens "foo" exclusively, or returns #f
(open-output-file "foo" :if-exists :append
:if-does-not-exist :error)
=> ;opens "foo" for append only if it already exists
|
To check the existence of a file without opening it,
use sys-access or file-exists? (See section 6.21.3.4 File stats).
Note: gauche.charconv module extends these procedures to
take encoding keyword argument so that they can read or write
in different character encoding scheme. See section 9.2 gauche.charconv - Character Code Conversion.
Note for portability: Some Scheme implementations (e.g. STk) allows
you to specify a command to filename and reads from, or
writes to, the subprocess standard input/output. Some other scripting
languages (e.g. Perl) have similar features. In Gauche,
open-input-file and open-output-file strictly operates
on files (what the underlying OS thinks as files).
However, you can use "process ports" to invoke
other command in a subprocess and to communiate it.
See section 9.14.2 Process ports, for details.
The keyword arguments if-exists, element-type and
if-does-not-exist have the same meanings of
open-input-file and open-output-file's. Note that
if you specify #f to if-exists and/or if-does-not-exist,
proc may receive #f instead of a port object when
the file is not opened.
Returns the value(s) proc returned.
The keyword arguments have the same
meanings of open-input-file and open-output-file's,
except that #f is not allowed to if-exists and
if-does-not-exist; i.e. an error is always signalled if the
file can't be opened.
Returns the value(s) thunk returned.
Notes on semantics of closing file ports:
R5RS states, in the description of call-with-input-file et al.,
that "If proc does not return, then the port will
not be closed automatically unless it is possible
to prove that the port will never again be used for read or write
operation."
Gauche's implementation slightly misses this criteria; the mere fact that an uncaptured error is thrown in proc does not prove the port will never be used. Nevertheless, it is very diffcult to think the situation that you can do meaningful operation on the port after such an error is signalled; you'd have no idea what kind of state the port is in. In practical programs, you should capture error explicitly inside proc if you still want to do some meaningful operation with the port.
Note that if a continuation captured outside call-with-input-file
et al. is invoked inside proc, the port is not closed.
It is possible that the control returns later into the proc,
if a continuation is captured in it (e.g. coroutines).
The low-level exceptions
(See section 6.16.4 Low-level exception mechanism) also doesn't ensure closing
the port.
open-input-file entry above; the default
is :full. Name is used for the created port's name
and returned by port-name. A boolean flag owner?
specfies whether fd should be closed when the port is closed.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |