Input/Output Command Summary

The following sections describe how to open, read, and write files. The basic model is that you open a file, read or write it, then close the file. Network sockets also use the commands described here. Socket programming is discussed in Chapter 17, and more advanced event-driven I/O is described in Chapter 16. Table 9-5 lists the basic commands associated with file I/O:

Table 9-5. Tcl commands used for file access.
open what ?access? ?permissions?Returns channel ID for a file or pipeline.
puts ?-nonewline? ?channel? stringWrites a string.
gets channel ?varname?Reads a line.
read channel ?numBytes?Reads numBytes bytes, or all data.
read -nonewline channelReads all bytes and discard the last \n.
tell channelReturns the seek ...

Get Practical Programming in Tcl & Tk, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.