好吧,我试图在网上找到我的答案,但实际上我没有,我真的需要帮助......
C:/Users/00_file/toto.odb, dis,455, stre,54, stra,25, C:/Users/00_file/tota.odb,
set Infile [open "C:/Users/00_file/file.txt" r] set filelines [split $Infile ","] set Namepath [lindex $filelines 1 0] #*doesn't work* set dis [lindex $filelines 2 0] # *work good* ...
问题是,当我想要使用我的TCL脚本完成文本文件的第1行时,一些信息会丢失并且额外的字符会消失...
如何获得完整的字符串(我的文本文件的第1行)?
非常感谢!
答案 0 :(得分:1)
您打开文件进行阅读,但实际上并未阅读该文件。 $ Infile只是(基本上)指向文件描述符的指针,而不是文件的内容:
% set fh [open file.txt r]
% puts $fh
file3
从文件中读取的惯用方法:逐行
set fh [open "C:/Users/00_file/file.txt" r]
set data [list]
while {[get $fh line] != -1} {
lappend data [split $line ,]
}
close $fh
或者,阅读整个文件并将其拆分为换行符
set fh [open "C:/Users/00_file/file.txt" r]
set data [lmap line [split [read -nonewline $fh] \n] {split $line ,}]
close $fh
然后访问数据
set Namepath [lindex $data 0 0] ;# first line, first field
set dis [lindex $data 1 1] ;# second line, second field
答案 1 :(得分:0)
Tcl代码如下:
set file [open c:/filename.txt ]
set file_device [read $file]
set data [split $file_device "\n"]
for {set count 0} {$count < 2} {incr count} {
puts $data
# for every iterartion one line will be printed.
# split /n is use for getting the end of each line.
# open command open the file at given path.
# read command is use to read the open file.
}
close $file
break
&#13;
这将是一个接一个的线。