New in version 6.3.0: See the new features.

Closing a sequential file, socket or pipe

fclose

Close a sequential file, socket or pipe


SYNTAX

fclose file_handle [err = label] [traps = label]

Closes the sequential file, socket or pipe that is currently open on a specified file_handle

file_handle

It can be the handle returned by the fopen command (it returns a negative number to differentitate the #channel case) or an integer in the range 1-64 specifying the number of the channel on which the sequential file, socket or pipe (opened by the open # command) is open. It may be a constant, field name or expression.

Channel 0 (zero) is the standard I/O channel and may not be closed.

[err = label]

Errors return the trappable condition err, which may be trapped by the err = clause or by the general traps = clause. The error number is stored in the system variable sys.Error , and control passes to the line indicated by label. The error numbers are defined by manifest constants in the file errors.h, which is located in the Sculptor include directory. This file may be included in a program by means of an !include declaration. The possible errors are:

No

Manifest constant

Meaning

1

BAD_CHANNEL

file_handle is wrong

13

CHECK_ERRNO

general error, check errno

If an error occurs and is not trapped, an error message is displayed and control passes to the active menu, if any. If no menu is active the program exits.

If an untrapped error occurs in a !report section, the fopen command is ignored.

[traps = label]

General purpose trap clause that traps any condition not explicitly trapped, passing control to the line indicated by label. It has the lowest priority, and is only invoked if a trappable condition has not been more explicitly trapped.


NOTES

  • If the handle/channel is already closed the command is ignored.

  • The number of files that may be open at the same time is operating system dependent.


EXAMPLE

A small example to create a file, write something and close it:

!temp fd,,i2

  fopen fd "test.txt" "w" traps=trap_ERR

  . Write some content with fputs
  fputs fd "Some text" traps=trap_ERR

  fclose fd traps=trap_ERR
  exit 0

trap_ERR:
  error "Error=" + tostr(sys.Error)

Using fclose to close an open # channel:

!temp fldHandle,,i4

fldHandle = 4
open #fldHandle, "file.txt" read
...
fclose fldHandle

RELATED TOPICS

fopen

close #

open #

close

Sequential files

Fcmds