代码见下,比较简单,就不再分析了。调用实例见前面的文章。
#************************************************
# get a line from file, skip blank lines and
# comment lines, return the reading line in
# parameter 'line'.
#
# @PARAMS
# fd - file fd
# line - var used to return the line
#
# @RETURN
# return 1 if read successfully, otherwise 0
#************************************************
proc getLine {fd line} {
upvar $line ln
# read a line from fd
while {[set lineLen [gets $fd ln]] >= 0} {
# blank line
if { $lineLen == 0 } continue
# trim whitespace
set ln [string trim $ln]
if { [string length $ln] == 0 } continue
# skip comment
if { [string index $ln 0] == "#" } continue
# success
return 1
}
return 0
}
# get a line from file, skip blank lines and
# comment lines, return the reading line in
# parameter 'line'.
#
# @PARAMS
# fd - file fd
# line - var used to return the line
#
# @RETURN
# return 1 if read successfully, otherwise 0
#************************************************
proc getLine {fd line} {
upvar $line ln
# read a line from fd
while {[set lineLen [gets $fd ln]] >= 0} {
# blank line
if { $lineLen == 0 } continue
# trim whitespace
set ln [string trim $ln]
if { [string length $ln] == 0 } continue
# skip comment
if { [string index $ln 0] == "#" } continue
# success
return 1
}
return 0
}
读取文件行
本文提供了一个简单的Tcl脚本函数getLine,用于从文件中读取一行并跳过空白行及注释行。该函数接收文件描述符和返回行变量作为参数,并返回读取状态。
1900

被折叠的 条评论
为什么被折叠?



