在tcl中向文件写入多行,如何添加空行和注释

在tcl中向文件写入多行,如何添加空行和注释,tcl,tk,Tcl,Tk,我正在tcl中向一个文件写入多行代码。虽然我写出来很成功,但也希望在几行之后有一个空行,并为每一行添加注释 puts $fileId "$tmpdir\t;# a comment and a blank line\n" puts $fileId "$tmpdir\n# a comment on its own line and then a blank line\n" puts $fileId "# a comment, a command invocation, and a blank l

我正在tcl中向一个文件写入多行代码。虽然我写出来很成功,但也希望在几行之后有一个空行,并为每一行添加注释

puts $fileId "$tmpdir\t;# a comment and a blank line\n"

puts $fileId "$tmpdir\n# a comment on its own line and then a blank line\n"

puts $fileId "# a comment, a command invocation, and a blank line\n$tmpdir\n"
这是我的密码

set tmpdir "set_var tmpdir  $tmpdir_path"
set vdd  "set vdd $voltage"
set gnd "set gnd 0.0"
set temp "set temp $temperature"
set rundir "set topdir $topdir"




set filename  "char_run.tcl"
set fileId [open $filename "w"]
puts $fileId $tmpdir
puts $fileId $vdd
puts $fileId $gnd
puts $fileId $rundir
close $fileId
请建议如何添加空行并对每行进行注释

puts $fileId "$tmpdir\t;# a comment and a blank line\n"

puts $fileId "$tmpdir\n# a comment on its own line and then a blank line\n"

puts $fileId "# a comment, a command invocation, and a blank line\n$tmpdir\n"
当然,你可以这样做:

lappend output "set_var tmpdir $tmpdir_path" "this is a temporary directory"                             0
lappend output "set vdd $voltage"            "voltage gets its name from Alessandro Volta (1745 – 1827)" 1
lappend output "set gnd 0.0"                 "that's the ground voltage"                                 1
lappend output "set temp $temperature"       "how hot or cold it is"                                     2
lappend output "set topdir $topdir"          "that's the base of the working directory tree"             0

set format "# %2\$s\n%1\$s%3\$s"
# or: set format "%1\$s\t;# %2\$s%3\$s"
# or: set format "%1\$s\n# %2\$s%3\$s"

foreach {cmd com nls} $output {
    puts $fileID [format $format $cmd $com [string repeat \n $nls]]
}
这样,您就得到了一个可以应用不同样式的输出数据库

文档:,,,

只需使用puts“”添加一个空行。或者,在某些文本后加上“\n”以添加换行符。写评论就像写任何其他行一样——只是行以散列开头




如果要编写Tcl文件,我建议使用
list
而不是字符串连接,例如
put$fileId[list set\u var tmpdir$tmpdir\u path
。这将生成一个有效的Tcl命令,如果路径中有一个空格,该命令不会阻塞。
% puts #line1; puts ""; puts line2
#line1

line2
%