在TCL中的文件中的特定位置插入文本

在TCL中的文件中的特定位置插入文本,tcl,Tcl,我的任务是从TCL脚本在文件中的任何给定位置插入文本 我尝试的是: set a [open "C:/TAT_DATA/temp.txt" a+] seek $a -8 end puts $a "insert between" close $a 但它正在替换/覆盖现有内容。我能做点别的吗? 我不想阅读整个文件内容。这可能是一种方式: set a [open "C:/TAT_DATA/temp.txt" a+] #go to the specific position seek $a -9 end

我的任务是从TCL脚本在文件中的任何给定位置插入文本

我尝试的是:

set a [open "C:/TAT_DATA/temp.txt" a+]
seek $a -8 end
puts $a "insert between"
close $a
但它正在替换/覆盖现有内容。我能做点别的吗?
我不想阅读整个文件内容。

这可能是一种方式:

set a [open "C:/TAT_DATA/temp.txt" a+]
#go to the specific position
seek $a -9 end
#read the data till end
set data [read $a]
#again move pointer to previous position
seek $a -9 end
#insert data (along with the previous data overwriting the existing data)
puts $a "insert between $data"
close $a

这可能是一种方式:

set a [open "C:/TAT_DATA/temp.txt" a+]
#go to the specific position
seek $a -9 end
#read the data till end
set data [read $a]
#again move pointer to previous position
seek $a -9 end
#insert data (along with the previous data overwriting the existing data)
puts $a "insert between $data"
close $a
fileutil
模块中的命令非常有用。您可以通过一个命令设置文件的内容:

writeFile $filename foobarfoobar
请看内容:

cat $filename
# -> foobarfoobar
添加更多内容:

appendToFile $filename baz
# file contents: foobarfoobarbaz
removeFromFile $filename 7 4
# file contents: foobar16foobarbaz
replaceInFile $filename 6 2 --==--
# file contents: foobar--==--foobarbaz
插入内容:

insertIntoFile $filename 6 123456
# file contents: foobar123456foobarbaz
删除内容:

appendToFile $filename baz
# file contents: foobarfoobarbaz
removeFromFile $filename 7 4
# file contents: foobar16foobarbaz
replaceInFile $filename 6 2 --==--
# file contents: foobar--==--foobarbaz
替换内容:

appendToFile $filename baz
# file contents: foobarfoobarbaz
removeFromFile $filename 7 4
# file contents: foobar16foobarbaz
replaceInFile $filename 6 2 --==--
# file contents: foobar--==--foobarbaz
或使用命令处理内容:

writeFile $filename "a b c"
proc foo data {
    foreach item $data {
        lappend res ($item)
    }
    join $res -
}
updateInPlace $filename foo
# file contents: (a)-(b)-(c)
文件:,包

fileutil
模块中的命令非常有用。您可以通过一个命令设置文件的内容:

writeFile $filename foobarfoobar
请看内容:

cat $filename
# -> foobarfoobar
添加更多内容:

appendToFile $filename baz
# file contents: foobarfoobarbaz
removeFromFile $filename 7 4
# file contents: foobar16foobarbaz
replaceInFile $filename 6 2 --==--
# file contents: foobar--==--foobarbaz
插入内容:

insertIntoFile $filename 6 123456
# file contents: foobar123456foobarbaz
删除内容:

appendToFile $filename baz
# file contents: foobarfoobarbaz
removeFromFile $filename 7 4
# file contents: foobar16foobarbaz
replaceInFile $filename 6 2 --==--
# file contents: foobar--==--foobarbaz
替换内容:

appendToFile $filename baz
# file contents: foobarfoobarbaz
removeFromFile $filename 7 4
# file contents: foobar16foobarbaz
replaceInFile $filename 6 2 --==--
# file contents: foobar--==--foobarbaz
或使用命令处理内容:

writeFile $filename "a b c"
proc foo data {
    foreach item $data {
        lappend res ($item)
    }
    join $res -
}
updateInPlace $filename foo
# file contents: (a)-(b)-(c)

文档:,包,,,,

您不必阅读整个文件内容,但您必须阅读其中的一些内容。我们如何才能做到这一点?我在下面介绍的方法或其他一些技术是他们的?我想你会发现操作系统并不像你想象的那样工作。插入(或删除)除文件末尾以外的文本需要重写。
fileutil
模块中的
insertIntoFile
命令可以满足您的需要。您不必阅读整个文件内容,但必须阅读其中的一些内容。我们如何才能做到这一点?我在下面介绍的方法或其他一些技术是他们的?我想你会发现操作系统并不像你想象的那样工作。插入(或删除)除文件末尾以外的文本需要重写。
fileutil
模块中的
insertIntoFile
命令可执行您想要的操作。