Guest_imported
New member
- Jan 1, 1970
- 0
Hi, I am new to Tcl/Tk. My problem is I need to insert data into a text file so that I can read from it in future. But how do I create a text file using tcl/tk? Thank you.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
# Open a file for writing. We'll assume that
# the file doesn't already exist. The return
# value of the open command is a file
# descriptor that we use as an argument for
# subsequent manipulation of the file.
set fid [open temp.txt w]
# Now, use puts commands to write data to
# the file. Tcl automatically determines the
# proper end-of-line convention for the
# operating system we're using.
puts $fid "Here's the first line."
puts -nonewline $fid "Here's a second line "
puts $fid "written in two separate chunks."
# Close the file when you're done.
close $fid
set fid [open temp.txt r]
# gets reads a line from the file indicated
# and stores the characters read (minus the
# end-of-line characters) in the variable
# provided. The return value of gets is the
# number of characters read, not including
# the end-of-line characters, or -1 if it
# reaches the end of file.
while {[gets $fid chars] >= 0} {
puts "Read line: $chars"
}
close $fid