How do I save the contents of a text widjet to a file?

0 views
Skip to first unread message

l...@writeme.com

unread,
Sep 9, 1999, 3:00:00 AM9/9/99
to
How do I save the contents of a text widjet to a file?


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

laurent....@cgi.ca

unread,
Sep 9, 1999, 3:00:00 AM9/9/99
to comp.l...@list.deja.com
On 9 Sep, l...@writeme.com wrote:
>
>
> How do I save the contents of a text widjet to a file?
>

Use text dump to get the contents of the text widget. See the man page for
that information (the man page should be available on all Unix platforms; a
.hlp file exists for Windows; an HTML version exists on Scriptics' site).

L

--
Penguin Power! Nothing I say reflects the views of my employer

Laurent Duperval mailto:laurent....@cgi.ca
CGI - FWFM Project Phone: (514) 391-9523

me

unread,
Sep 9, 1999, 3:00:00 AM9/9/99
to
set data [widgetname get 1.0 end]
set fp [open outputfile w]
puts $fp $data
close $fp

<l...@writeme.com> wrote in message news:7r6vt8$cpa$1...@nnrp1.deja.com...


> How do I save the contents of a text widjet to a file?
>
>

Bryan Oakley

unread,
Sep 17, 1999, 3:00:00 AM9/17/99
to
> > How do I save the contents of a text widjet to a file?
> >
>
> set data [widgetname get 1.0 end]
> set fp [open outputfile w]
> puts $fp $data
> close $fp

That is not quite correct. You will end up with *2* unwanted trailing
newlines.

You want "end-1c" since tk always adds a trailing newline in the text
widget. Also, you want to do "puts -nonewline", or puts will add a
second newline.

Andreas Leitgeb

unread,
Sep 20, 1999, 3:00:00 AM9/20/99
to
Bryan Oakley <boa...@vignette.com> wrote:
>> puts $fp $data

> That is not quite correct. You will end up with *2* unwanted trailing
> newlines.

a text-file should always have a \n at the end of each (including the last)
line. otherwise, it's not a proper textfile.

you're right, that puts -nonewline is better, here.
the other newline, however, is "there to stay" ;-)


Bryan Oakley

unread,
Sep 20, 1999, 3:00:00 AM9/20/99
to
Andreas Leitgeb wrote:
>
> Bryan Oakley <boa...@vignette.com> wrote:
> >> puts $fp $data
> > That is not quite correct. You will end up with *2* unwanted trailing
> > newlines.
>
> a text-file should always have a \n at the end of each (including the last)
> line. otherwise, it's not a proper textfile.

Wrong. At least partially so. You should never just blindly save
everything in the text widget. To properly save data you *must* use
"end-1c", or be prepared for the consequences.

If you want to force a trailing newline, you must take care to do it
properly and not let it get magically and invisibly added. Left
unabated, using [.text get 1.0 end] as the basis of saving the contents
to a file will cause one additional newline to be added to the file each
time the file is loaded and subsequently saved. In a Real Program this
would be very annoying indeed.

The following code illustrates the point. It uses the wrong method of
saving the data to a file, causing it to grow by one line with each
iteration. After 100 write/read cycles the file will contain 101 lines
even though the original data was only one line. If you really believe
all text files should end with a newline, it is much better to simply
check the final character before writing to a file, and adding a newline
IFF there is none.

# this lets you re-"source" this code in a console window
# as often as you wish.
eval destroy [winfo children .]

# create our sample widgets
label .l
frame .f
text .t -yscrollcommand {.vsb set}
scrollbar .vsb -command {.t yview}

pack .l -side top -fill x -expand n
pack .f -side top -fill both -expand y
pack .t -in .f -side left -fill both -expand y
pack .vsb -in .f -side right -fill y -expand n

raise .
update idletasks

# insert some arbitrary text....
.t insert end "this is a single line of text"

# now, save and restore, improperly, that single line of text and
# watch it grow!
for {set i 1} {$i <= 100} {incr i} {
# get the current contents of the text widget

# change which of the following two lines gets executed
# to see how the alternate method works. Note that using
# "end" instead of "end-1c" appends a newline to the file
# for each read/write iteration.
set contents [.t get 1.0 end]
# set contents [.t get 1.0 end-1c]

# save the data to a file, being careful to not add
# any additional newlines via the puts statement.
set file [open "./sample.txt" w]
puts -nonewline $file $contents
close $file

# clear out the contents of text widget
.t delete 1.0 end

# read the saved data back in and display it in the text widget
set file [open "./sample.txt" r]
set contents [read $file]
.t insert end $contents
close $file

# update the label to show how many iterations we've performed,
# and how many lines are in the file
set nlines [expr int([.t index {end-1c}])]
.l configure -text "iteration: $i number of lines in the widget
$nlines"
update idletasks
}


>
> you're right, that puts -nonewline is better, here.
> the other newline, however, is "there to stay" ;-)

I suggest you reconsider that last statement if you don't want to add a
bunch of extraneous newlines to a file.

Andreas Leitgeb

unread,
Sep 21, 1999, 3:00:00 AM9/21/99
to
Bryan Oakley <boa...@vignette.com> wrote:
> Wrong. At least partially so. You should never just blindly save
> everything in the text widget. To properly save data you *must* use
> "end-1c", or be prepared for the consequences.
> ...

> I suggest you reconsider that last statement if you don't want to add a
> bunch of extraneous newlines to a file.

Somehow, I obviously missed the fact, that the text _always_ added
a newline. For dubious reasons I thought, it would only add a newline
if the last line is unfinished. *sorry*

If text adds too many \n's (bug or feature), then I agree, they must
be removed. (either the bug or the newline) ;-)


Reply all
Reply to author
Forward
0 new messages