I/O

  • Syntax and behavior are similar to the C I/O library
  • UNIX-like commands: cd, pwd
  • File handling: open, close
  • I/O : gets, puts
  • Information and misc: eof, file, glob
  • Pre-defined standard files: stdin, stdout, stderr
cd [dirName]
Usual UNIX syntax
pwd
Usual UNIX syntax
open fileName [accessType]
Opens the file named fileName
Returns a file identifier (the fileId of gets, puts,...)
accessType could be
r
Read Only. File must exist. Default.
r+
Read and Write. File must exist.
w
Write Only. Truncates the file if already exists. Otherwise creates a new one.
w+
Write and Read. Truncates if exists. Otherwise creates a new one.
a
Write only. Append if exists. Otherwise creates a new one.
a+
Write and Read. Append if exists. Otherwise creates a new one.
Example:
% set fp [open $fname r]
close fileId
Close the file given by fileId and returns an empty string.
Example:
close $fp
gets fileId [varName]
Reads the next line from the file
Stores the line into varName
Returns the number of characters, or -1 for end of file (eof)
If varName is not provided, returns the line or an empty string for end of file
Removes the terminating newline
Example:
set fp [open $fname r]
while { [gets $fp line] >= 0 } {
    parse_line $line
}
gets stdin a                                ;# reads from stdin
puts [-nonewline] [fileId] string
Writes string into fileId
If fileId is not provided, stdout is assumed
Appends a newline unless the -nonewline option is used
Returns an empty string
eof fileId
Returns 1 if an end-of-file condition has occurred on fileId. 0 otherwise.
file { exists|rootname|dirname|tail|extension ...} args ...
A family of commands to get information about files and directories.
Example:
set setupName  setup.ini
if { [file exists $setupName] } {
    read_setup $setupName
}
set extension [file extension $setupName]        ;# gets ".ini"
glob [-nocomplain] [--] pattern [pattern ...]
Return a list of files that match any of the patterns
Uses glob pattern matching technique
If -nocomplain isn't specified, an error occurs if nothing matches
Use - to say that the next argument is the pattern (if it begins with a dash)
Example:
set DESIGN dummy
foreach sfx  { sp st* pa* lis tr* mt* ac* ma* sw* ms* } {
    foreach file [glob -nocomplain "$DESIGN.$sfx"] {
        VovInput $file
    }
}