我想逐行读取cd.txt中的值,并将每个值都视为变量。
# Damper Properties
set Kd 0.000001;
set Cd [open "CD.txt" r];
set ad 0.000001;
if [catch {open $CD.txt r} cd] { ; # Open the input file and check for error
puts stderr "Cannot open $inFilename for reading"; # output error statement
} else {
foreach line [[read $cd] \n] { ; # Look at each line in the file
if {[llength $line] == 0} { ; # Blank line -> do nothing
continue;
} else {
set Xvalues $line; # execute operation on read data
}
}
close $cd; ; # Close the input file
}
# Define ViscousDamper Material
#uniaxialMaterial ViscousDamper $matTag $Kd $Cd $alpha
uniaxialMaterial ViscousDamper 1 $Kd $Cd $ad
里面有什么问题? cd.txt中的值是一个十进制值。循环不起作用。请帮忙。
答案 0 :(得分:0)
此行:
foreach line [[read $cd] \n] { ; # Look at each line in the file
缺少关键位。这个版本看起来更正确:
foreach line [split [read $cd] \n] { ; # Look at each line in the file
(我希望您确实要做的不仅仅是将Xvalues
设置到每个非空行,因为它本身不太可能有用。)
答案 1 :(得分:0)
您打开了错误的文件:
set Cd [open "CD.txt" r];
Cd
变量现在包含一个文件句柄,其值类似于“ file3421”。然后你做
if [catch {open $CD.txt r} cd] { ; # Open the input file and check for error
您现在正尝试打开文件“ file3421.txt”-希望您在此出现“找不到文件”错误。
此外,您还应将表达式括起来:
if {[catch {open "CD.txt" r} cd]} { ...
#..^............................^
从文件读取行的惯用方式是:
while {[gets $cd line] != -1} { ...