BlueScript: Standard Streams
Contents
Description
Common functions for creating streams (files, commands, stdio).
Documentation
These functions are not included in a standard BlueScript
interpreter by default. Check the documentation for your
specific interpreter implementation to see if they are included.
Procedures
fopen
Usage
fopen filename [mode]
Returns
A new stream object which points at the contents of the
given file.
Opens a file for reading or writing as a stream object. The
mode corresponds to the modes used in the Standard C
fopen()
function:
r
- opens a file for reading at the
beginning.
r+
- opens a file for reading and
writing at the beginning.
w
- truncates or creates a file for writing.
w+
- truncates or creates a file for
reading and writing.
a
- opens or creates a file for writing
at the end.
a+
- opens or creates a file for reading
and writing at the end.
If mode is omitted then the mode defaults to r
.
Examples
1. Opening a file for reading
fopen /etc/passwd r
Result: stream
popen
Usage
popen cmd [mode]
Executes a command and allows the script to read from its
standard output and/or write to its standard intput. The
mode parameter determines what kind of input/output is
supported:
r
- allow reading from command's standard
output
w
- allow writing to command's standard
input
If mode is omitted then it defaults to r
.
This command depends upon the presence and functionality of
the underlying popen()
C library function. On
some platforms (e.g. Windows) the availability and
functionality of this procedure may be constrained.
Examples
1. Reading a command's output
popen {grep ^root
/etc/passwd} r
Result: stream
stderr
Usage
stderr
Returns
A write-only stream object corresponding to standard error.
stdin
Usage
stdin
Returns
A read-only stream object corresponding to standard input.
stdio
Usage
stdio
Returns
A read-write stream object where input is taken from
standard input and output is written to standard output.
stdout
Usage
stdout
Returns
A write-only stream object corresponding to standard output.