The fclose function may be used to close an open file pointer
fp. Upon success it returns zero, and upon failure it sets
errno and returns -1. Failure usually indicates a that
the file system is full or that fp does not refer to an open file.
Notes
Many C programmers call fclose without checking the return
value. The S-lang language requires the programmer to explicitly
handle any value returned by a S-lang function. The simplest way to
handle the return value from fclose is to use it as:
Convert a FD_Type file descriptor to a stdio File_Type object
Usage
File_Type fdopen (FD_Type, String_Type mode)
Description
The fdopen function creates and returns a stdio
File_Type object from the open FD_Type
descriptor fd. The mode parameter corresponds to the
mode parameter of the fopen function and must be
consistent with the mode of the descriptor fd. The function
returns NULL upon failure and sets errno.
Notes
The fclose function does not close the File_Type object
returned from this function. The underlying file object must be
closed by the close function.
This function may be used to determine the state of the end-of-file
indicator of the open file descriptor fp. It returns 0
if the indicator is not set, or non-zero if it is. The end-of-file
indicator may be cleared by the clearerr function.
Determine the error status of an open file descriptor
Usage
Integer_Type ferror (File_Type fp)
Description
This function may be used to determine the state of the error
indicator of the open file descriptor fp. It returns 0
if the indicator is not set, or non-zero if it is. The error
indicator may be cleared by the clearerr function.
The fflush function may be used to update the output
stream specified by fp. It returns 0 upon success, or
-1 upon failure and sets errno accordingly. In
particular, this function will fail if fp does not represent
an output stream, or if fp is associated with a disk file and
there is insufficient disk space.
Example
This example illustrates how to use the fflush function
without regard to the return value:
fgets reads a line from the open file specified by fp
and places the characters in the variable whose reference is
specified by ref.
It returns -1 if fp is not associated with an open file
or an attempt was made to read at the end the file; otherwise, it
returns the number of characters read.
Example
The following example returns the lines of a file via a linked list:
The fgetslines function returns all the remaining lines as an
array of strings in the file specified by the open file pointer
fp. If the file is empty, an empty string array will be
returned. The function returns NULL upon error.
Example
The following function returns the number of lines in a file:
The fopen function opens a file f according to the mode
string m. Allowed values for m are:
"r" Read only
"w" Write only
"a" Append
"r+" Reading and writing at the beginning of the file.
"w+" Reading and writing. The file is created if it does not
exist; otherwise, it is truncated.
"a+" Reading and writing at the end of the file. The file is created
if it does not already exist.
In addition, the mode string can also include the letter 'b'
as the last character to indicate that the file is to be opened in
binary mode.
Upon success, fopen a File_Type object which is meant to
be used in other operations that require an open file. Upon
failure, the function returns NULL.
Example
The following function opens a file in append mode and writes a
string to it:
define append_string_to_file (file, str)
{
variable fp = fopen (file, "a");
if (fp == NULL) verror ("%s could not be opened", file);
() = fputs (string, fp);
() = fclose (fp);
}
Note that the return values from fputs and fclose are
ignored.
Notes
There is no need to explicitly close a file opened with fopen.
If the returned File_Type object goes out of scope, S-lang
will automatically close the file. However, explicitly closing a
file after use is recommended.
The fputs function writes the string s to the open file
pointer fp. It returns -1 upon failure and sets errno,
otherwise it returns the length of the string.
Example
The following function opens a file in append mode and uses the
fputs function to write to it.
define append_string_to_file (str, file)
{
variable fp;
fp = fopen (file, "a");
if (fp == NULL) verror ("Unable to open %s", file);
if ((-1 == fputs (s, fp))
or (-1 == fclose (fp)))
verror ("Error writing to %s", file);
}
Notes
One must not disregard the return value from the fputs
function, as many C programmers do. Doing so may lead to a stack
overflow error.
To write an object that contains embedded null characters, use the
fwrite function.
UInt_Type fread (Ref_Type b, DataType_Type t, UInt_Type n, File_Type fp)
Description
The fread function may be used to read n objects of type
t from an open file pointer fp. Upon success, it
returns the number of objects read from the file and places the
objects in the variable specified by b. Upon error or end of
file, it returns -1. If more than one object is read from the
file, those objects will be placed in an array of the appropriate
size. The exception to this is when reading Char_Type or
UChar_Type objects from a file, in which case the data will be
returned as a BString_Type binary string.
Example
The following example illustrates how to read 50 bytes from a file:
The fseek function may be used to reposition the file position
pointer associated with the open file stream fp. Specifically,
it moves the pointer ofs bytes relative to the position
indicated by whence. If whence is set to one of the symbolic
constants SEEK_SET, SEEK_CUR, or SEEK_END, the
offset is relative to the start of the file, the current position
indicator, or end-of-file, respectively.
The function return zero upon success, or -1 upon failure and sets
errno accordingly.
The current implementation uses an integer to specify the offset.
One some systems, a long integer may be required making this
function fail for very large files, i.e., files that are longer than
the maximum value of an integer.
The ftell function may be used to obtain the current position in the
stream associated with the open file pointer fp. It returns
the position of the pointer measured in bytes from the beginning of
the file. Upon error, it returns -1 and sets errno.
The fwrite may be used to write the object represented by
b to an open file. If b is a string or an array, the
function will attempt to write all elements of the object to the
file. It returns the number of objects successfully written,
otherwise it returns -1 upon error and sets errno
accordingly.
Example
The following example illustrates how to write an integer array to a
file. In this example, fp is an open file descriptor:
variable a = [1:50]; % 50 element integer array
if (50 != fwrite (a, fp))
error ("fwrite failed");
Here is how to write the array one element at a time:
variable a = [1:50];
foreach (a)
{
variable ai = ();
if (1 != fwrite(ai, fp))
error ("fwrite failed");
}
Notes
Not all data types may support the fwrite operation. However,
it is supported by all vector, scalar, and string objects.
The popen function executes a process specified by cmd
and opens a unidirectional pipe to the newly created process. The
mode indicates whether or not the the pipe is open for reading
or writing. Specifically, if mode is "r", then the
pipe is opened for reading, or if mode is "w", then the
pipe will be open for writing.
Upon success, a File_Type pointer will be returned, otherwise
the function failed and NULL will be returned.
fprintf formats the objects specified by the variable argument
list according to the format fmt and write the result to
stdout. This function is equivalent to fprintf used
with the stdout file pointer. See fprintf for more
information.
printf returns the number of characters written to the file,
or -1 upon error.
Notes
Many C programmers do not check the return status of the
printf C library function. Make sure that if you do not care
about whether or not the function succeeds, then code it as in the
following example:
() = printf ("%s laid %d eggs\n", chicken_name, num_egg);