Sequential files¶
A sequential file, pipe or socket can be opened from within a Sculptor program, and data read from or written to it. When the channel is opened it is allocated a channel number in the range 1-64. All subsequent operations on the file (or socket, or pipe) refer to it by means of this channel number.
Sculptor commands that perform operations on channels end with the # character, e.g. check #, getfield #. The # character is immediately followed by the required channel number. For example:
check #22
checks for available bytes in the file open on channel 22.
The Sculptor commands and functions relevant to channels are:
Check if bytes are available in a sequential file, socket or pipe |
|
Close a sequential file, socket or pipe |
|
Execute another program |
|
Flush pending output to a specified channel |
|
Read data into fields from a sequential file, socket or pipe |
|
Return the ASCII value of the next character from a channel |
|
Open a sequential file, socket or pipe |
|
Write data to a sequential file, socket or pipe |
|
Rewind a sequential file to the beginning |
|
Position a sequential file at a stated position |
|
Return the current position of a sequential file |
|
Position a sequential file at the end |
Channel 0¶
Channel 0 is a special case, used by Sculptor to refer to stdin (the standard input channel) and stdout (the standard output channel).
Standard input is the keyboard, unless input has been redirected from a file.
Standard output is the screen, unless redirected to a file.
So:
get #0 |
reads data from stdin |
put #0 |
writes data to stdout |
Sculptor does not permit channel 0 to be opened or closed. It allows it to be used if it is open when the program starts. This is operating-system dependent, as follows:
MS-DOS, UNIX |
stdin and stdout are always open, and default to the terminal. Both stdin and stdout can be redirected from/to a file. If a DOS program is running under Windows, it must be running in a DOS box, which represents the terminal. |
Windows |
stdin and stdout are closed. The only exception occurs when the program has been started from a DOS command and the channel has been redirected from or to a file. Provided that these two conditions are true, the file can be read using get[field] #0 and written to using put[field] #0. Note however that inside a !report section, put #0 sends its output to the printer. |
The open # command can be used to open the standard input, output and error channels.
Fcmds and file handles¶
New in version 6.3.0: See the new features.
This new group of commands can be used with files, pipes or sockets and work in a similar way to their C counterparts:
Close a sequential file, socket or pipe |
|
Read a newline terminated string |
|
Open a sequential file, socket or pipe |
|
Writing formatted data to a sequential file |
|
Write a newline terminated string |
|
Read binary data from a sequential file, socket or pipe |
|
Rewind a sequential file to the beginning |
|
Position a sequential file at a stated position |
|
Writing binary data to a sequential file, socket or pipe |
The Fcmds work with file handles but also support Sculptor sequential channels (as the normal channel commands support file handles). To differentiate between file handles and channels, the file handles are stored as negative number while the channels as positive.
Warning
Due the use of negative numbers for file handles, remember to use a signed integer to store file handles (i1, i2, i3, i4 or i8)
So for example fprintf
into standard output can be done using the Channel 0:
!temp str,,a20,,v = "World"
fprintf 0 "Hello %!" str
The opposite is also possible, use a fopen
’ed stdout file handle with the classical channel commands:
!temp str,,a20,,v = "World"
!temp fHandle,,i4
fopen fHandle "stdout" "a"
put #fHandle, "Hello " + str +"!"
Communication between programs¶
Using the execute() function, another program can be executed and comunicate with it through specified channels (pipes)
Apart from this, under all operating systems, it is possible to run two console programs with a pipe between them:
program1 | program2
This directs stdout of program1 to stdin of program2. Now, if program1 uses put[field] #0 and program2 get[field] #0, data can be sent from program1 to program2. Note that this flow of communication can travel in that direction only.
The stdin = and stdout = clauses of the exec command enable the standard input and/or output of the child task to be redirected to designated channel numbers. This, in effect, creates a two-way communication.
Note
A console program is one that runs from the operating system’s command line, in character-mode, rather than from a graphical user interface.