89.05%
Search · Index

V.9 File Operations


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.

 

Reference

  • file atime filename
    Returns as a decimal number the time that the file was last accessed.
    set access_time [file atime "index.adp"] ==> 916612934 
    
  • file dirname filename
    Returns the name of the parent directory of the file.
    set parent_dir [file dirname "~/home/dir/this.adp"] ==> ~/home/dir
    
  • file executable filename
    Returns 1 if the file is executable, 0 otherwise.
    chmod 1111 billg-wealth.tcl
    file executable billg-wealth.tcl ==> 1
  • file exists filename
    Returns 1 if the file exists, 0 otherwise.
    file exists billg-wealth.tc ==> 0
    file exists billg-wealth.tcl ==> 1
    
  • file extension filename
    Returns the file extension of the file (i.e. from the last dot to the end)
    file extension billg-wealth.tcl ==> .tcl
    
  • file isdirectory filename
    Returns 1 if the file is a directory, 0 otherwise.
    file isdirectory . ==> 1
    file isdirectory billg-wealth.tcl ==> 0
    
  • file isfile filename
    Returns 1 if the file is not a directory, symbolic link, or device, 0 otherwise.
    file isfile billg-wealth.tcl ==> 1
    
  • file lstat filename variablename
    Puts the results of the stat command on linkname into variablename.
    ln -s billg-wealth.tcl temp.tcl
    file lstat temp.tcl temp ==> (array holding stat info)
    
  • file mtime filename
    Returns the modify time of file as a decimal string.
    file modify billg-wealth.tcl ==> 915744902
    
  • file owned filename
    Returns 1 if the current user owns the file, else 0.
    file owned billg-wealth.tcl ==> 1
    
  • file readable filename
    Returns 1 if the file is readable, else 0.
    file readable billg-wealth.tcl ==> 1
    
  • file readlink filename
    Returns the contents of the symbolic link named filename.
    ln -s file.txt file1.txt
    file readlink file1.txt ==> file.txt
    
  • file rootname filename
    Returns all but the extension and the last . of the filename.
    file rootname billg-wealth.tcl ==> billg-wealth
    
  • file size filename
    Returns the size in bytes of the file.
    file size billg-wealth.tcl ==> 774
    
  • file stat filename variablename
    Returns the stat results about the file into the array named variablename. The elements of the variable array are: atime, ctime, dev, gid, ino, mode, mtime, nlink, size, type, and uid.
    file stat billg-wealth.tcl billg_info 
    set $billg_info(ctime) ==> 916615489
  • file tail filename
    Returns all of the characters after the last / in the filename.
    file tail ~/home/dir/subdir/file.txt ==> file.txt
    
  • file type filename
    Returns the type identified of the filename arg, which can be one of the following: file, directory, characterSpecial, blockSpecial, fifo, link, or socket.
    file type billg-wealth.tcl ==> file
    
  • file writable filename
    Returns 1 if the file is writable, 0 otherwise.
    file writable billg-wealth.tcl ==> 0
    

More: http://www.tcl.tk/man/tcl8.4/TclCmd/file.htm

 

 

Input/Output Commands

  • open filename ?access? ?permissions?
    Returns a stream handle to open the file for the access specified with the permissions specified. Defaut values are read for the access required and the permissions are the same as the default permissions on a file. The access value options are r (read from existing file), r+ (read from and write to existing file), w (write over or create and write to file as necessary), w+ (read from and write to or create file as necessary), a (write to existing file; append data to it), a+ (read from and write to existing file; append data to it).
    set my_stream [open /tmp/file.txt r]
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/open.htm

  • puts ?-nonewline? ?stream? string
    Write the string to the stream. Default is STDOUT.
    puts "Hello, world." ==> Hello, world.
    
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/puts.htm

  • gets stream ?varname?
    Read a line from the stream. If a variable is specified, put the line into that variable.
    gets $my_stream line
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/gets.htm

  • read stream ?numbytes?
    If numbytes is specified, read that many bytes of the stream. If not, read the whole stream.
    set first_ten_bytes [read $my_stream 10]
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/read.htm

  • read -nonewline stream
    Read the whole stream and discard the last newline.
    set this_file_contents [read -nonewline $my_stream]
  • tell stream
    Return the "seek offset." (See below for seek.)
    set seek_offset [tell $my_stream]
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/tell.htm

  • seek stream offset ?origin?
    Set the seek offset. Origin is either start, current, or end.
    seek $my_stream offset end
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/seek.htm

  • eof stream
    Returns 1 if you have reached the end of the stream; 0 otherwise.
    if {[eof $my_stream]} {
    break
    }
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/eof.htm

  • flush stream
    Write buffers of a stream
    flush $my_stream
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/flush.htm

  • close stream
    Close the stream.
    close $my_stream
    More: http://www.tcl.tk/man/tcl8.4/TclCmd/close.htm

 

 


 

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