如何在tcl mql中读取和写入文件?

时间:2014-12-01 05:45:06

标签: tcl

我想从包含名称的文本文件中读取,我想检查名称是否包含字母a。如果是这样,那么我希望输出在out.txt文件中为y else n

inputs 

noman 
Lily 

####################

outputs 

y
n 

2 个答案:

答案 0 :(得分:1)

> set fp [open "input.txt" "r+"] ; set out [open "output.txt" "w"]
>          #loop through warehouse item rel and disconnect
>          while { [gets $fp line] >= 0 } {
>     puts $out $line ; }
> 
> close $fp close $out;

您需要在上面的代码中添加条件行:

while { [gets $fp line] >= 0 } {
    if {[regexp {a} $line]} {
       puts $out "y"
    } else {
       puts $out "n"
    }
}

答案 1 :(得分:0)

set fp [open "input.txt" "r+"]
set out [open "output.txt" "w"]
         #loop through warehouse item rel and disconnect
         while { [gets $fp line] >= 0 } {
    puts $out $line ;
}

close $fp
close $out;

这有效