Why can't i use same file channel twice in TCL

manju sharma

I was writing a TCL program which looked something like this :

#!/usr/bin/tclsh
set fInp [open file1.txt r]
while {[gets $fInp line] >= 0} {
      statement 1
      statement 2
}

statement 3
statement 4

while {[gets $fInp line] >=0} {
      statement 5
      statement 6
}

close $fInp

I was expecting this to work fine , but to my surprise , the second while loop was not getting executed at all. I came to a conclusion that we cannot read a file in TCL twice using same file descriptor (or channel)

So I closed the fInp and opened that file again using fInp2 , and it worked !

What is the reason behind this behavior , and is there any other way of doing it ?

Thanks

Shawn

This is normal behavior for reading from files in every programming language and OS I'm familiar with. Once you read to the end of the file in the first loop, there's nothing left to read. You can reset and adjust the internal offset into the file's contents using the seek command, though.

seek $fInp 0 start

after the first loop will reset it to the beginning of the file so you can read it again in the second loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't I iterate twice over the same data?

Why can't I iterate twice over the same data?

Why can't I call read() twice on an open file?

Why doesn't the goroutines process the same file twice?

Why Go's channel can close twice?

Why same Go Channel can't be started twice?

Why can't I use a "file" as the "body" of an http request?

How can I use the same widget twice in one page

Why I can't reuse WebClient to make the same request twice?

Why can't write internal flash of same address twice

Why can't I use assert with std::is_same?

Why can't you increment/decrement a variable twice in the same expression?

Why I can't change a bool twice?

can't use the same function twice

Why can't I use the same Get-Date variable twice without getting incorrect time

Why can't i receive twice from the same client

Why can't I access the correct value of pi in tcl?

Why can't I have a folder and a file with the same name?

Can I Use the Set Command Twice With the Same Variable Name?

Why can I free memory twice, but can't in different situations?

Why can't I use the same method twice for one instance in ruby?

Can I use the same Ansible handler twice in one playbook execution?

Why can't I use res.json() twice in one post request?

why can't I pass the return twice?

Why can't I use the same variable again in Django?

Why can't I use the same parser twice in a tuple()?

Why can't I use the same span ids in an html page?

Why can't I use match method twice in if...else construction?

Why can't I join an open channel using telethon?