TCL脚本文件读取和写入--实操版

linux下执行TCL脚本

你的linux装了tcl解释器了吗,一般是 /usr/bin/tclsh,如果确认你装了 (确认方法是在命令行下执行 tclsh),如果没问题,就在你的TCL脚本第一行加上

#!/usr/bin/env tclsh

然后确保你的脚本(假定脚本名字叫test.tcl) 是可执行的 (chmod +x test.tcl)
然后直接运行 ./test.tcl 即可。

实操一:每次读入固定字符数量

#Set the length of the data read each time
set LENGTH	256
#Set input and output file names
set FILE_IN	./temp_in.txt
set FILE_OUT ./temp_out.txt
if { $argc > 1 } {
	#user input file_in name
	set FILE_IN [lindex $argv 0]
	#user input file_out name
	set FILE_OUT [lindex $argv 1]
}
#open file for reading
set file_input [open $FILE_IN r]
#open file for writing
set file_output [open $FILE_OUT w+]
#
fconfigure $file_input -translation binary -encoding binary -buffering full
#print execution time
set SystenTime [clock seconds]
puts "start time ...... [clock format $SystenTime -format %D:%H:%M:%S]"
while { 1 } {
	set buffer [read $file_input $LENGTH]
	puts -nonewline $file_output $buffer
	if { [ eof $file_input ] } {
		break
	}
}
close $file_input
close $file_output

实操二:每次读入一行数据

#Set the length of the data read each time
set LENGTH	256
#Set input and output file names
set FILE_IN	./temp_in.txt
set FILE_OUT ./temp_out.txt
if { $argc > 1 } {
	#user input file_in name
	set FILE_IN [lindex $argv 0]
	#user input file_out name
	set FILE_OUT [lindex $argv 1]
}
#open file for reading
set file_input [open $FILE_IN r]
#open file for writing
set file_output [open $FILE_OUT w+]
#
fconfigure $file_input -translation binary -encoding binary -buffering full
#print execution time
set SystenTime [clock seconds]
puts "start time ...... [clock format $SystenTime -format %D:%H:%M:%S]"
while { ![ eof $file_input ] } {
	gets $file_input line
	puts $file_output $line
}
close $file_input
close $file_output
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值