Tool command language(Tcl)
3. 其它有用的Tcl命令
TCAD Sentaurus工具引入更高级的Tcl命令
3.1文件输入和输出
-
使用open函数打开文件:
set FIDw [open Writing.tmp "w"]
这里以写的方式打开文件Writing.tmp。FIDw是一个常规的Tcl变量,它包含文件标识符。
-
使用put 命令写入打开的文件:
puts $FIDw "This is the first line of this file"
puts $FIDw $ABCList
- 使用close命令关闭文件:
close $FIDw
- 以读的方式打开文件
set FIDr [open Writing.tmp "r"]
- gets命令逐行读取文件
while { [gets $FIDr NewLine] > 0 } { #NewLine是固定必须的关键词?
puts $NewLine
}
close $FIDr
#-> This is the first line of this file
#-> a b c d e f
这里,while循环检测文件的结尾。gets命令返回读取的字节数。如果gets到达文件末尾,它将返回-1.while循环将测试此条件。
- 使用read命令将文件作为单个数据块读取
set FIDr [open Writing.tmp "r"]
set DATA [read $FIDr]
puts $DATA
close $FIDr
#-> This is the first line of this file
#-> a b c d e f
包含特殊Tcl字符的文件通常无法逐行读取。read命令没有这些问题。
3.2 格式化输出
使用格式函数控制打印期间变量的格式:
set pi [ expr 2.0*asin(1.0) ]
puts "pi unformated :$pi"
#-> pi unformated: 3.141592
puts "pi with 2 digits :[format %.2f $pi]"
#-> pi with 2 digits : 3.14
puts "pi in exponential format: [format %.4e $pi]"
#-> pi in exponential format: 3.1416e+00
set i 24
puts "Integer with leading zeros: >[format %05d $i]<"
#-> Integer with leading zeros: >00