f



How to read a file using TCL

Hi,
   I am a new bee to TCL.  I want to write a script that opens the
file and reads it and prints to screen.  I have tried the below. But
doesn't works.

set INFILE [open "temp" a+]
set data [read $INFILE]
set data [split $data "\n"]
foreach line $data {
          puts $line
     }

-Regards
Swaroop Kumar Tata

0
5/14/2007 8:39:41 AM
comp.lang.tcl 23428 articles. 2 followers. Post Follow

10 Replies
1477 Views

Similar Articles

[PageSpeed] 21

On 14 Mai, 10:39, Swaroop <swaroop.t...@gmail.com> wrote:
> Hi,
>    I am a new bee to TCL.  I want to write a script that opens the
> file and reads it and prints to screen.  I have tried the below. But
> doesn't works.
>
> set INFILE [open "temp" a+]

You're opening in "a" (append) mode, that is, the file pointer puts at
the end. As you want to read the file, why not use mode "r", which is
default anyway? So just do
   set INFILE [open temp]


0
5/14/2007 9:20:49 AM
On May 14, 2:20 pm, suchenwi <richard.suchenwirth-
bauersa...@siemens.com> wrote:
> On 14 Mai, 10:39, Swaroop <swaroop.t...@gmail.com> wrote:
>
> > Hi,
> >    I am a new bee to TCL.  I want to write a script that opens the
> > file and reads it and prints to screen.  I have tried the below. But
> > doesn't works.
>
> > set INFILE [open "temp" a+]
>
> You're opening in "a" (append) mode, that is, the file pointer puts at
> the end. As you want to read the file, why not use mode "r", which is
> default anyway? So just do
>    set INFILE [open temp]


Hi,
   Its working when i use "r" mode.  But i am writing a script that
has the choices of adding a new entry to the file. Searching some
record in the file.  For that i have opend in a+ mode.  Do i need to
open and close for each function.  I mean if i am appending a line,
open file with a+ and close it.  if i want to read it open file with
"r" and close it...?

-Swaroop

0
5/14/2007 10:02:37 AM
On 14 Mai, 12:02, Swaroop <swaroop.t...@gmail.com> wrote:

>    Its working when i use "r" mode.  But i am writing a script that
> has the choices of adding a new entry to the file. Searching some
> record in the file.  For that i have opend in a+ mode.  Do i need to
> open and close for each function.  I mean if i am appending a line,
> open file with a+ and close it.  if i want to read it open file with
> "r" and close it...?

No. "a+" is append (write) as well as read. Just if you want to mix
modes, you should use [seek] to "move the pointer":
seek $INFILE 0       ;# before reading from beginning
seek $INFILE 0 end ;# before appending to end



0
5/14/2007 10:12:09 AM
On May 14, 3:12 pm, suchenwi <richard.suchenwirth-
bauersa...@siemens.com> wrote:
> On 14 Mai, 12:02, Swaroop <swaroop.t...@gmail.com> wrote:
>
> >    Its working when i use "r" mode.  But i am writing a script that
> > has the choices of adding a new entry to the file. Searching some
> > record in the file.  For that i have opend in a+ mode.  Do i need to
> > open and close for each function.  I mean if i am appending a line,
> > open file with a+ and close it.  if i want to read it open file with
> > "r" and close it...?
>
> No. "a+" is append (write) as well as read. Just if you want to mix
> modes, you should use [seek] to "move the pointer":
> seek $INFILE 0       ;# before reading from beginning
> seek $INFILE 0 end ;# before appending to end


Thanks Suchenwi...It works......

0
5/14/2007 10:14:19 AM
Swaroop wrote:
> Hi,
>    I am a new bee to TCL.  I want to write a script that opens the
> file and reads it and prints to screen.  I have tried the below. But
> doesn't works.
> 
> set INFILE [open "temp" a+]
> set data [read $INFILE]
> set data [split $data "\n"]
> foreach line $data {
>           puts $line
>      }

This is because the a and a+ modes seek to the end of the file. You 
would have to do:
  set INFILE [open "temp" a+]
  seek $INFILE 0 start
  ...
which is the same as just doing
  set INFILE [open "temp" w+]

However, if you are either reading the file in one place or appending to 
it in another then it is easier to use two separate [open]s which each 
do just what they need to (i.e. "r" in one place and "a" in another).

-- Neil
0
nem3909 (1000)
5/14/2007 10:21:32 AM
On 14 May 2007 03:12:09 -0700, 
suchenwi <richard.suchenwirth-bauersachs@siemens.com> wrote:

> No. "a+" is append (write) as well as read. Just if you want to mix
> modes, you should use [seek] to "move the pointer":
> seek $INFILE 0       ;# before reading from beginning
> seek $INFILE 0 end ;# before appending to end

Do you actually need to seek to the end of the file to write?

I had the impression that in append mode an inherent seek is ALWAYS
performed prior to any write, usually by the OS itself.  Or does TCL
simply open the file for writing as with w or w+, and move the file
pointer to the end once at the start?

In the documentation I was just looking at, the description of APPEND
for the C API version of the [open] command seems to suggest an "always
seek to end" approach.  So I'm wondering just what the deal is?

Personally, I'd love it if a+ would hold the file pointer still during
a write, and just "append" the data to the end of the file.


Fredderic
0
5/14/2007 10:22:28 AM
Fredderic wrote:
> On 14 May 2007 03:12:09 -0700, 
> suchenwi <richard.suchenwirth-bauersachs@siemens.com> wrote:
> 
>> No. "a+" is append (write) as well as read. Just if you want to mix
>> modes, you should use [seek] to "move the pointer":
>> seek $INFILE 0       ;# before reading from beginning
>> seek $INFILE 0 end ;# before appending to end
> 
> Do you actually need to seek to the end of the file to write?
> 
> I had the impression that in append mode an inherent seek is ALWAYS
> performed prior to any write, usually by the OS itself.  Or does TCL
> simply open the file for writing as with w or w+, and move the file
> pointer to the end once at the start?
> 
> In the documentation I was just looking at, the description of APPEND
> for the C API version of the [open] command seems to suggest an "always
> seek to end" approach.  So I'm wondering just what the deal is?

[open $file APPEND] will seek to end before each write, whereas [open 
$file a/a+] will just seek to end initially, as stated in the open.n 
manpage.

-- Neil
0
nem3909 (1000)
5/14/2007 11:36:02 AM
On Mon, 14 May 2007 12:36:02 +0100, 
Neil Madden <nem@cs.nott.ac.uk> wrote:

> > I had the impression that in append mode an inherent seek is ALWAYS
> > performed prior to any write, usually by the OS itself.  Or does TCL
> > simply open the file for writing as with w or w+, and move the file
> > pointer to the end once at the start?
> [open $file APPEND] will seek to end before each write, whereas [open 
> $file a/a+] will just seek to end initially, as stated in the open.n 
> manpage.

I didn't realise those keyword modes worked in the TCL [open] command
also...  Okay...  To set things straight...

If a/a+ mode, it does one seek to the end, and then behaves exactly
like w/w+ mode would.  Where the word "APPEND" invokes the full
seek-to-end-every-write behaviour...?

So how do they both behave if you're writing, for example, to a log
file which is also being updated by another process?

I'm guessing the a/a+ would start over-writing another process's
additions, whereas the APPEND keyword would do what I'd expect, and
actually append even if something else has come along and added stuff
to the end in the meantime.

Or am I still utterly and hopelessly confused?  ;)


Fredderic
0
5/14/2007 11:53:20 AM
Fredderic wrote:
....
> If a/a+ mode, it does one seek to the end, and then behaves exactly
> like w/w+ mode would.  Where the word "APPEND" invokes the full
> seek-to-end-every-write behaviour...?

Yes.

> So how do they both behave if you're writing, for example, to a log
> file which is also being updated by another process?
> 
> I'm guessing the a/a+ would start over-writing another process's
> additions, whereas the APPEND keyword would do what I'd expect, and
> actually append even if something else has come along and added stuff
> to the end in the meantime.

Yes. Which is why all logging utilities (should) use {APPEND CREAT 
WRONLY}. See http://wiki.tcl.tk/1241 for a discussion, when that site 
becomes available again (or use Google cache).

-- Neil
0
nem3909 (1000)
5/14/2007 12:06:05 PM
In article <1179136957.617119.157810@e65g2000hsc.googlegroups.com>,
Swaroop  <swaroop.tata@gmail.com> wrote:
>On May 14, 2:20 pm, suchenwi <richard.suchenwirth-
>bauersa...@siemens.com> wrote:
>> On 14 Mai, 10:39, Swaroop <swaroop.t...@gmail.com> wrote:
>>
>> > Hi,
>> >    I am a new bee to TCL.  I want to write a script that opens the
>> > file and reads it and prints to screen.  I have tried the below. But
>> > doesn't works.
>>
>> > set INFILE [open "temp" a+]
>>
>> You're opening in "a" (append) mode, that is, the file pointer puts at
>> the end. As you want to read the file, why not use mode "r", which is
>> default anyway? So just do
>>    set INFILE [open temp]
>
>
>Hi,
>   Its working when i use "r" mode.  But i am writing a script that
>has the choices of adding a new entry to the file. Searching some
>record in the file.  For that i have opend in a+ mode.  Do i need to
>open and close for each function.  I mean if i am appending a line,
>open file with a+ and close it.  if i want to read it open file with
>"r" and close it...?
			.
			.
			.
It occurs to me that, after you've digested the excellent advice
given in other follow-ups, <URL: http://wiki.tcl.tk/17396 > might
be of interest.
0
claird (2363)
5/14/2007 2:42:39 PM
Reply: