www.delorie.com/gnu/docs/guile/guile_317.html   search  
 
Buy GNU books!


Guile Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

27.9.1 File Ports

The following procedures are used to open file ports. See also open, for an interface to the Unix open system call.

Scheme Procedure: open-file filename mode
C Function: scm_open_file (filename, mode)
Open the file whose name is filename, and return a port representing that file. The attributes of the port are determined by the mode string. The way in which this is interpreted is similar to C stdio. The first character must be one of the following:
`r'
Open an existing file for input.
`w'
Open a file for output, creating it if it doesn't already exist or removing its contents if it does.
`a'
Open a file for output, creating it if it doesn't already exist. All writes to the port will go to the end of the file. The "append mode" can be turned off while the port is in use see section fcntl
The following additional characters can be appended:
`+'
Open the port for both input and output. E.g., r+: open an existing file for both input and output.
`0'
Create an "unbuffered" port. In this case input and output operations are passed directly to the underlying port implementation without additional buffering. This is likely to slow down I/O operations. The buffering mode can be changed while a port is in use see section setvbuf
`l'
Add line-buffering to the port. The port output buffer will be automatically flushed whenever a newline character is written.
In theory we could create read/write ports which were buffered in one direction only. However this isn't included in the current interfaces. If a file cannot be opened with the access requested, open-file throws an exception.

Scheme Procedure: open-input-file filename
Open filename for input. Equivalent to
 
(open-file filename "r")

Scheme Procedure: open-output-file filename
Open filename for output. Equivalent to
 
(open-file filename "w")

Scheme Procedure: call-with-input-file file proc
proc should be a procedure of one argument, and file should be a string naming a file. The file must already exist. These procedures call proc with one argument: the port obtained by opening the named file for input or output. If the file cannot be opened, an error is signalled. If the procedure returns, then the port is closed automatically and the value yielded by the procedure is returned. If the procedure 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 a read or write operation.

Scheme Procedure: call-with-output-file file proc
proc should be a procedure of one argument, and file should be a string naming a file. The behaviour is unspecified if the file already exists. These procedures call proc with one argument: the port obtained by opening the named file for input or output. If the file cannot be opened, an error is signalled. If the procedure returns, then the port is closed automatically and the value yielded by the procedure is returned. If the procedure 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 a read or write operation.

Scheme Procedure: with-input-from-file file thunk
thunk must be a procedure of no arguments, and file must be a string naming a file. The file must already exist. The file is opened for input, an input port connected to it is made the default value returned by current-input-port, and the thunk is called with no arguments. When the thunk returns, the port is closed and the previous default is restored. Returns the value yielded by thunk. If an escape procedure is used to escape from the continuation of these procedures, their behavior is implementation dependent.

Scheme Procedure: with-output-to-file file thunk
thunk must be a procedure of no arguments, and file must be a string naming a file. The effect is unspecified if the file already exists. The file is opened for output, an output port connected to it is made the default value returned by current-output-port, and the thunk is called with no arguments. When the thunk returns, the port is closed and the previous default is restored. Returns the value yielded by thunk. If an escape procedure is used to escape from the continuation of these procedures, their behavior is implementation dependent.

Scheme Procedure: with-error-to-file file thunk
thunk must be a procedure of no arguments, and file must be a string naming a file. The effect is unspecified if the file already exists. The file is opened for output, an output port connected to it is made the default value returned by current-error-port, and the thunk is called with no arguments. When the thunk returns, the port is closed and the previous default is restored. Returns the value yielded by thunk. If an escape procedure is used to escape from the continuation of these procedures, their behavior is implementation dependent.

Scheme Procedure: port-mode port
C Function: scm_port_mode (port)
Return the port modes associated with the open port port. These will not necessarily be identical to the modes used when the port was opened, since modes such as "append" which are used only during port creation are not retained.

Scheme Procedure: port-filename port
C Function: scm_port_filename (port)
Return the filename associated with port. This function returns the strings "standard input", "standard output" and "standard error" when called on the current input, output and error ports respectively.

Scheme Procedure: set-port-filename! port filename
C Function: scm_set_port_filename_x (port, filename)
Change the filename associated with port, using the current input port if none is specified. Note that this does not change the port's source of data, but only the value that is returned by port-filename and reported in diagnostic output.

Scheme Procedure: file-port? obj
C Function: scm_file_port_p (obj)
Determine whether obj is a port that is related to a file.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003