File handling
Now that we can open, read, write, and close a file, it is time to put this knowledge to work in a real world manner. We will now create a Tcl script that accesses a file, reorders the stored data, and then outputs it to a secondary file. This is similar to a file handling procedure used to clean up user supplied files.
Getting ready
Using the text editor of your choice create a text file containing the following text:
1,3,5,7,8,2,4,6,9
Save the file in your working directory as input.txt
.
How to do it…
Using the text editor of your choice create the following Tcl script and save it in your working directory as filehandler.tcl
.
# Check that a filename was provided if { $argc>0 & $argv>0} { # Assign the filename to a variable set fname [lindex $argv 0] # Open the file for read-only access set fp [open $fname r] # Read the contents of the file into a variable set data [read $fp] #Close the input file close $fp # Split the data and create a Tcl list set input [split $data...