Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newlines after using get command

Status
Not open for further replies.

Malachi

Programmer
Aug 13, 2001
38
US
I'm using the following command to write text to a file. However, after executing this procedure, the saved text appears to have two newlines appended to the text.


proc saveFile { file } {
set f [open $file w+]
puts $f [.textEntry get 1.0 end]
close $f
}


Here is the proc to open the file for input...


proc openFile { file } {
set f [open $file]
.textEntry delete 1.0 end
.textEntry insert end [read $f]
close $f
}


How can I save the file without the newlines appended?

Brad

 
the line:
puts $f [.textEntry get 1.0 end]
gets the line with a newline appended.
try:
puts $f [.textEntry get 1.0 "end - 1 char"]

ulis
 
Malachi: Every line inserted into a Text widget must end in a newline (\n). When the following line executes:

Code:
puts $f [.textEntry get 1.0 end]

you're writing the terminating \n from the last line of the Text widget, and you're getting the \n that puts automatically writes at the end of the string.

The easiest way to fix this is to use puts -nonewline to suppress the \n typically added by puts:

Code:
puts -nonewline $f [.textEntry get 1.0 end]
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top