免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8076 | 回复: 6
打印 上一主题 下一主题

[求助]tcl中文件读写的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-11 12:39 |只看该作者 |倒序浏览
我想打开一个文件,然后比如读到第2行,在后面加used,可发现used总是加到文件的最后,这是为什么呢?我打印了文件的偏移量,是第二行的结尾阿

#!/opt/tools/bin/tclsh

set fname {/home/leozhu/AlphaNumber}


set f [open $fname r+]

puts "hello"

while { [gets $f line]>=0 } {
  if {[regexp {used} $line]} {
    continue
    } else {
    scan $line "%s" PhoneNumber
    puts stdout $PhoneNumber
    set pos  [tell $f]
    puts $pos
    set $f $pos
    puts $f used
    break
   }
}
puts "end"
close $f
exit 0

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2007-10-11 13:47 |只看该作者
set $f $pos 这是啥意思?

论坛徽章:
0
3 [报告]
发表于 2007-10-11 15:39 |只看该作者
不好意思,写错了,应该是
seek $f $pos

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
4 [报告]
发表于 2007-10-11 15:56 |只看该作者
如果你想puts在gets之后,直接 seek $f 0 current 试试

另外这是不是个bug是没必要讨论的,tcl的open/puts/gets是建立在系统调用的基础上的,在俺的机器上man fopen 中有这样的一段

       Reads and writes may be intermixed on read/write streams in any order.
       Note  that  ANSI C requires that a file positioning function intervene
       between output and input, unless an input operation encounters end-of-
       file.  (If this condition is not met, then a read is allowed to return
       the result of writes other than the most  recent.)   Therefore  it  is
       good  practice  (and indeed sometimes necessary under Linux) to put an
       fseek or fgetpos operation between write and read operations on such a
       stream.
  This operation may be an apparent no-op (as in fseek(..., 0L,
       SEEK_CUR) called for its synchronizing side effect.

论坛徽章:
0
5 [报告]
发表于 2007-10-11 17:35 |只看该作者
这样直接在原文件上添加数据是不可行的

论坛徽章:
0
6 [报告]
发表于 2007-10-11 21:20 |只看该作者
多谢楼主!现在改用另外一种方法了,直接跳过原来读的内容,这样就不用在读过的内容上加标记了。

proc readfile {} {

global pos
global fname
global numberphone

set f [open $fname r+]

if {$numberphone > 0} {
set pos  [expr [expr $numberphone*6] +$numberphone]
}

seek $f $pos

while { [gets $f line]>=0 } {
    scan $line "%s" PhoneNumber
    puts stdout $PhoneNumber
    break
   }
close $f

incr numberphone 1
}

论坛徽章:
0
7 [报告]
发表于 2007-10-12 08:51 |只看该作者
原帖由 leo314034 于 2007-10-11 21:20 发表
多谢楼主!现在改用另外一种方法了,直接跳过原来读的内容,这样就不用在读过的内容上加标记了。

楼主者谁?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP