14
File Commands and Functions
This chapter contains information on the following commands:
close
close(
p_port
)
=> t
Description
Drains, closes, and frees a port.
When a file is closed, it frees the FILE* associated with p_port. Do not use this function on piport, stdin, poport, stdout, or stderr.
Arguments
Value Returned
Example
p = outfile( "~/test/myFile" ) => port:"~/test/myFile"
close( p )
=> t
Drains, closes, and frees the /test/myFile port.
fscanf
fscanf(p_inputPort t_formatString[s_var1... ] ) =>x_items/ nil
Description
Reads input from a port according to format specifications and returns the number of items read in.
The results are stored into corresponding variables in the call. The fscanf function can be considered the inverse function of the fprintf output function. The fscanf function returns the number of input items it successfully matched with its format string. It returns nil if it encounters an end of file.
The maximum size of any input string being read as a string variable for fscanf is currently limited to 8 K. Also, the function lineread is a faster alternative to fscanf for reading Virtuoso® SKILL objects.
The common input formats accepted by fscanf are summarized below.
| Format Specification | Types of Argument | Scans for |
|---|---|---|
Arguments
Value Returned
|
Returns the number of input items it successfully read in. As a side effect, the items read in are assigned to the corresponding variables specified in the call. |
|
Example
fscanf( p "%d %f" i d )
Scans for an integer and a floating-point number from the input port p and stores the values read in the variables i and d, respectively.
Assume a file testcase with one line:
hello 2 3 world
x = infile("testcase")
=> port:"testcase"
fscanf( x "%s %d %d %s" a b c d )
=> 4
(list a b c d) => ("hello" 2 3 "world")
gets
gets(s_variableName[p_inputPort] ) =>t_string/ nil
Description
Reads a line from the input port and stores the line as a string in the variable. This is a macro.
The string is also returned as the value of gets. The terminating newline character of the line becomes the last character in the string.
Arguments
Value Returned
|
Returns |
Example
Assume the test1.data file has the following first two lines:
#This is the data for test1
0001 1100 1011 0111
p = infile("test1.data") => port:"test1.data"
gets(s p) => "#This is the data for test1"
gets(s p) => "0001 1100 1011 0111"
s => "0001 1100 1011 0111"
Gets a line from the test1.data file and stores it in the variable s. The s variable contains the last string stored in it by the gets function.
infile
infile(S_fileName) =>p_inport/ nil
Description
Opens an input port ready to read a file.
Always remember to close the port when you are done. The file name can be specified with either an absolute path or a relative path. In the latter case, the current SKILL path is used if it is not nil.
Arguments
|
Name of the file to be read; it can be either a string or a symbol. |
Value Returned
|
Returns |
Example
in = infile( "~/test/input.il" ) => port:"~/test/input.il"
close( in )
=> t
Closes the /test/input.il port.
Opens the input port /test/input.il.
infile("myFile") => nil
Returns nil if myFile does not exist according to the current setting of the SKILL path or exists but is not readable.
load
load(t_fileName[t_password] ) => t
Description
Opens a file and repeatedly calls lineread to read in the file, immediately evaluating each form after it is read in.
This function uses the file extension to determine the language mode (.il for SKILL, .ils for SKILL++, and .ocn for a file containing OCEAN commands) for processing the language expressions contained in the file. For a SKILL++ file, the loaded code is always evaluated in the top-level environment.
load closes the file when the end of file is reached. Unless errors are discovered, the file is read in quietly. If load is interrupted by pressing Control-c, the function skips the rest of the file being loaded.
SKILL has an autoload feature that allows applications to load functions into SKILL on demand. If a function being run is undefined, SKILL checks to see if the name of the function (a symbol) has a property called autoload attached to it. If the property exists, its value, which must be either a string or an expression that evaluates to a string, is used as the name of a file to be loaded. The file should contain a definition for the function that triggered the autoload. Processing proceeds normally after the function is defined.
Arguments
|
File to be loaded. Uses the file name extension to determine the language mode to use. Valid values: |
|
Value Returned
Example
load( "test.ocn" )
procedure( trLoadSystem()
load( "test.il" ) ;;; SKILL code
load( "test.ils" );;; SKILL++ code
) ; procedure
You might have an application partitioned into two files. Assume that test.il contains SKILL code and test.ils contains SKILL/SKILL++ code. This example loads both files.
newline
newline(
[ p_outputPort ]
)
=> nil
Description
Prints a newline (backslash n) character and then flushes the output port.
Arguments
Value Returned
Example
print( "Hello" ) newline() print( "World!" )
"Hello"
"World!"
=> nil
Prints a newline character after Hello.
outfile
outfile(S_fileName[t_mode] ) =>p_outport/ nil
Description
Opens an output port ready to write to a file.
Various print commands can write to this file. Commands write first to a character buffer, which writes to the file when the character buffer is full. If the character buffer is not full, the contents are not written to the file until the output port is closed or the drain command is entered. Use the close or drain command to write the contents of the character buffer to the file. The file can be specified with either an absolute path or a relative path. If a relative path is given and the current SKILL path setting is not nil, all directory paths from SKILL path are checked in order, for that file. If found, the system overwrites the first updatable file in the list. If no updatable file is found, it places a new file of that name in the first writable directory.
Arguments
|
Mode in which to open the file. If |
Value Returned
|
returns |
Example
p = outfile( "/tmp/out.il" "w" )
=> port:"/tmp/out.il"
outfile( "/bin/ls" )
=>nil
Returns nil, indicating that the specified port could not be opened.
pfile
pfile( [S_fileName|p_port] ) =>p_port/ nil
Description
Opens an output port ready to write to a file or returns the name of an existing port indicating that it is available.
This command is similar to the outfile command when a valid S_fileName is specified. When p_port is specified, it returns the file port that is currently being used by p_port. When no argument is specified, it opens the stdout port.
Arguments
Value Returned
Example
p = pfile( "/tmp/out.il" "w" )
=> port:"/tmp/out.il"
pfile( "/bin/ls" )
=>nil
Returns nil, indicating that the specified port could not be opened.
p = pfile()
=> port:"*stdout*"
Returns stdout as the file port indicating that stdout has been opened.
pfile( p )
=> port:"/tmp/out.il"
printf
printf(t_formatString[g_arg1... ] ) => t
Description
Writes formatted output to poport, which is the standard output port.
The optional arguments following the format string are printed according to their corresponding format specifications. Refer to the Common Output Format Specifications table for fprintf in the Cadence SKILL Language User Guide.
printf is identical to fprintf except that it does not take the p_port argument and the output is written to poport.
Arguments
Value Returned
Example
x = 197.9687 => 197.9687
printf( "The test measures %10.2f." x )
Prints the following line to poport and returns t.
The test measures 197.97. => t
println
println(g_value[p_outputPort] ) => nil
Description
Prints a SKILL object using the default format for the data type of the value, and then prints a newline character.
A newline character is automatically printed after printing g_value. The println function flushes the output port after printing each newline character.
Arguments
Value Returned
Example
for( i 1 3 println( "hello" ))
"hello"
"hello"
"hello"=> t
Prints hello three times. fort.
Return to top