Using files and keyboard input

From NEST

Jump to: navigation, search

[ An Introduction to SLI ]

Contents

Overview

SLI's input/output fascilities differ from those of PostScript and are close to the stream concept of C++. However, for compatibility some PostScript output commands are implemented.

Like in C++, files are represented as streams, which can be put on the stack. All i/o commands leave their stream argument on the stack, so that operations can be chained.

The command < takes the role of C++'s << output operator.

Example

Print (Hello World) to the standard output.

SLI ] cout (Hello World) <- endl
Hello World
SLI [1] ;
SLI ]

cout is the standard output of SLI. The command <- takes the role of C++'s << output operator and prints the ASCII representation of the object at stack level 0 to the stream at level 1. After this, the object is removed and the stream remains at level 0.

The command endl corresponds to the C++ manipulator of the same name. It prints an end of line character to the stream at level 0. Again, it leaves the stream argument on the stack.

Now, the abbreviated form of pop, i.e. the command ;, is used to remove the stream object from the stack.

Standard streams

The standard streams of a UNIX program are mapped to the following names. Note that these streams should not be closed by a SLI program, since the result is undefined.

NameDescription
cin Standart input stream.
cout Standart output stream.
cerr Standart error output stream.

Opening and closing a stream

Streams are objects which handle the input and output of data to or from some external target. The target of a stream can be a file, a string, a devide, or another process.

CommandDescription
(name) (r) file Open file for reading.
(name) (w) file Open file for writing.
stream close Close the stream.
CommandDescription
(name) ifstream Open file for reading.
(name) ofstream Open file for writing.
(string) istrstream Open a string-stream for reading.
ostrstream Open a string-stream for writing.
strstream Extract a string from a string-stream.

Writing to streams

CommandDescription
stream obj <- Print ASCII representation of obj to stream.
stream obj <-- Print detailed ASCII representation of obj to stream.
obj = Print ASCII representation of obj to cout.
obj == Print detailed ASCII representation of obj to cout.

Example

Print (Hello World) to a text file.

 SLI ] (test.txt) (w) file 
 SLI [1] (Hello World!) <- 
 SLI [1] endl 
 SLI [1] ;


Manipulators

Manipulators are used to manipulate the state of a stream object. Such changes can, for instance, affect the precision with which numbers are printed.

Manipulators take one or more arguments. In general, the manipulator leaves the stream object at the top of the stack and removes all other arguments.

ManipulatorDescription
ofstream flush write contents of buffer to file
ofstream endl line terminator
osstream ends char[] string terminator
ifstream ws eat white-spaces
ofstream boolalpha Prints bool as true/false
ofstream noboolalphaopposite
fstream n setw set width of input/output fields to n
stream (c) setfill defines a fill symbol for the field
ostream left allign to left of the field
ostream right allign to right of the field
ostream internal sign left and number right
ostream showpos print positive sign
ostream noshowpos opposite
ostream uppercase
ostream nouppercase
ostream octSwitch to octal notation
ostream dec Switch to decimal notation
ostream hex Switch to hexadecimal notation
ostream showbase show base according to use of oct/dec/hex
ostream noshowbase
ostream showpoint decimal point is allways printed
ostream noshowpoint
ostream n setprecision Set number of decimal places to n.
ostream fixed Use fixed point notation
ostream scientific Use scientific notation
Views
Personal tools