Tcl has a built-in interface for dealing with Unix files. The commands themselves are relatively straightforward, so we'll just explain them in a reference list below.
file atime filenameset access_time [file atime "index.adp"] ==> 916612934
file dirname filenameset parent_dir [file dirname "~/home/dir/this.adp"] ==> ~/home/dir
file executable filenamechmod 1111 billg-wealth.tcl
file executable billg-wealth.tcl ==> 1
file exists filename file exists billg-wealth.tc ==> 0 file exists billg-wealth.tcl ==> 1
file extension filenamefile extension billg-wealth.tcl ==> .tcl
file isdirectory filenamefile isdirectory . ==> 1 file isdirectory billg-wealth.tcl ==> 0
file isfile filenamefile isfile billg-wealth.tcl ==> 1
file lstat filename variablenamestat command on linkname into variablename. ln -s billg-wealth.tcl temp.tcl file lstat temp.tcl temp ==> (array holding stat info)
file mtime filename file modify billg-wealth.tcl ==> 915744902
file owned filenamefile owned billg-wealth.tcl ==> 1
file readable filenamefile readable billg-wealth.tcl ==> 1
file readlink filenameln -s file.txt file1.txt file readlink file1.txt ==> file.txt
file rootname filenamefile rootname billg-wealth.tcl ==> billg-wealth
file size filenamefile size billg-wealth.tcl ==> 774
file stat filename variablenamefile stat billg-wealth.tcl billg_info
set $billg_info(ctime) ==> 916615489
file tail filenamefile tail ~/home/dir/subdir/file.txt ==> file.txt
file type filenamefile type billg-wealth.tcl ==> file
file writable filenamefile writable billg-wealth.tcl ==> 0
More: http://www.tcl.tk/man/tcl8.4/TclCmd/file.htm
open filename ?access? ?permissions?More: http://www.tcl.tk/man/tcl8.4/TclCmd/open.htmset my_stream [open /tmp/file.txt r]
puts ?-nonewline? ?stream? stringMore: http://www.tcl.tk/man/tcl8.4/TclCmd/puts.htmputs "Hello, world." ==> Hello, world.
gets stream ?varname?More: http://www.tcl.tk/man/tcl8.4/TclCmd/gets.htmgets $my_stream line
read stream ?numbytes?More: http://www.tcl.tk/man/tcl8.4/TclCmd/read.htmset first_ten_bytes [read $my_stream 10]
read -nonewline streamset this_file_contents [read -nonewline $my_stream]
tell streamMore: http://www.tcl.tk/man/tcl8.4/TclCmd/tell.htmset seek_offset [tell $my_stream]
seek stream offset ?origin?start, current, or end. More: http://www.tcl.tk/man/tcl8.4/TclCmd/seek.htmseek $my_stream offset end
eof streamMore: http://www.tcl.tk/man/tcl8.4/TclCmd/eof.htmif {[eof $my_stream]} {
break
}
flush streamMore: http://www.tcl.tk/man/tcl8.4/TclCmd/flush.htmflush $my_stream
close streamMore: http://www.tcl.tk/man/tcl8.4/TclCmd/close.htmclose $my_stream
Exercises
1. Write a procedure to check the spelling of a word:
# spell :: filedescriptor X string -> boolean
proc spell {fd word} {...}
It should take a file descriptor, which refers to an opened dictionary file, and a string, which is the word to be checked, and it should return 1 if the word is spelled correctly, or 0 if not.
We define spelled correctly to mean that the word is listed in the dictionary file. We ignore the issues of plurals, etc: if the exact word is not found in the dictionary, it's misspelled.
We define a dictionary file to be any file that contains words, one per line, which is sorted in ASCII collating order. You may use the file /home/keith/web/tcl-course/words for testing.
Hint: this proc is extremely easy to write. If you disagree, you're approaching it wrong.
2. Modify your spelling checker to output an identifying line number with each mispelled word. Your output for each line should be the line number, a tab, and the mispelled word.
Hint: this should add about two lines to your program.
---
based on Tcl for Web Nerds