# 打印行范围 set file [open "filename.txt" r] set lineNumber 0 while {[gets $file line] != -1} { incr lineNumber if {$lineNumber >= 5 && $lineNumber <= 10} { puts $line } } close $file # 创建变量 set variableName "value" puts $variableName ...
和gets命令相似,但也有自己的形式。第一种形式是读并返回fileId标识的文件中所有剩下的字节,如果没有设置nonewline,则在换行符处停止;第二种形式是在fileId标识的文件中读并返回下一个numBytes字节。 puts命令: puts ?-nonewline? ?fileId? string puts命令把string写到fileId中,如果没设置nonewline,会添加换...
语法功能: gets读fileld标识的文件的下一行,并把该行赋给变量,并返回该行的字符数(文件尾返回 -1) close 语法格式: close fileid 语法功能: 关闭文件 #读取文件 (Desktop) 52 % set INPUTFILE [open file.txt r] file1a75e298e40 (Desktop) 53 % while {[gets $INPUTFILE line] >=0 } { > puts...
flush fileld把缓冲区内容写到fileld标识的文件中,命令返回值为空字符串。 flush命令迫使缓冲区数据写到文件中,flush直到数据被写完才返回。当文件关闭时缓冲区数据会自动flush。 proc tgrep { pattern filename} { set f [open $filename r] while { [gets $f line ] } { if {[regexp $ pattern $line...
gets $rfile_handle line get fileID line就是读取fileID文件的下一行,忽略换行符,将该行的内容赋值给line if {[string match"*VIOLATION*" $line]} { 如果line中含有VIOLATION字符 putswfilehandleline 将string写到fileId中,此处是将上面读到的行信息给只写文件写入 ...
set file [open "example.txt" "r"]while {[gets $file line] != -1} { # 处理每一行的操作 puts "Line: $line"} # 检查是否已经到达文件末尾 if {[eof $file]} { puts "Reached end of file"} close $file 在这个例子中,open 命令用于打开一个文件,并使用 gets 命令逐行读取文件内容。eof ...
puts -nonewline $f “hello\n” puts $f “world” close $f puts -nonewline $f “hello\n”表示的是强制不换行打印,否则自动追加一个换行符 文件系统 基本常用操作: gets –》 一次读一行文件 puts –》 写入文件 open –》 打开文件 close –》 关闭文件 ...
set f [open $filename r] while { [gets $f line ] } { if {[regexp $pattern $line]} { puts stdout $line } } close $f } open命令返回一个字符串用于表识打开的文件。当调用别的命令(如:gets,puts,close,〕对打开的文件进行操作时,就可以使用这个文件标识符。TCL有三个特定的文件标识:stdin...
# Read a line from the file and analyse it. gets $fid line if { [regexp {^rectangle +([0-9]+) +([0-9]+) +([0-9]+) +([0-9]+) +([0-9]+) +(.*)$} $line dummy x1 y1 x2 y2 thickness color] } { .c create rectangle $x1 $y1 $x2 $y2 -width $thickness -out...
set file [open "filename.txt" r] while {[gets $file line] != -1} { set words [split $line " "] foreach word $words { if {[string match "*keyword*" $word]} { puts $line break } } } close $file 上述代码首先打开指定的文件,然后逐行读取文件内容。对于每一行,它将行内容...