ZetCode

Tcl gets Command

last modified April 3, 2025

The Tcl gets command reads a line of input from a file or standard input. It's commonly used for reading user input or processing files line by line. The command is essential for interactive Tcl programs.

Basic Definition

The gets command reads a line from a file channel and stores it in a variable. It returns the number of characters read or -1 at end of file.

Syntax: gets channelId ?varName?. With one argument, it returns the line read. With two arguments, it stores the line in the variable.

Reading from Standard Input

This example shows how to read user input from the console using gets.

basic_gets.tcl
puts "Enter your name:"
gets stdin name
puts "Hello, $name!"

This prompts the user to enter their name. gets reads from stdin and stores the input in the name variable. The greeting is then printed.

Reading a File Line by Line

gets is commonly used to process files line by line in a loop.

file_gets.tcl
set file [open "data.txt" r]
while {[gets $file line] >= 0} {
    puts "Read: $line"
}
close $file

This opens a file for reading and processes each line. The loop continues until gets returns -1 (EOF). Each line is printed with a prefix.

Checking for End of File

The return value of gets can be used to detect end of file conditions.

eof_gets.tcl
set file [open "notes.txt" r]
while {true} {
    set status [gets $file line]
    if {$status == -1} break
    puts "Line length: [string length $line]"
}
close $file

This example explicitly checks the return value of gets. When -1 is returned, the loop breaks. Otherwise, it prints the length of each line.

Reading Without Storing

gets can be used without storing the result when you just want to skip lines.

skip_gets.tcl
set file [open "largefile.txt" r]
# Skip first 5 lines
for {set i 0} {$i < 5} {incr i} {
    gets $file
}
# Now process the rest
while {[gets $file line] >= 0} {
    puts $line
}
close $file

This demonstrates skipping lines by calling gets without a variable name. The first 5 lines are discarded, then the rest are processed normally.

Reading Fixed Number of Characters

By combining gets with other commands, you can read specific amounts of data.

fixed_gets.tcl
set file [open "data.bin" r]
fconfigure $file -translation binary
set chunk [read $file 1024]
while {[string length $chunk] > 0} {
    puts "Read [string length $chunk] bytes"
    set chunk [read $file 1024]
}
close $file

While gets reads lines, this shows an alternative for fixed-size reads. The file is configured for binary mode and read in 1024-byte chunks.

Interactive Input with Timeout

gets can be combined with fileevent for non-blocking input.

timeout_gets.tcl
proc readInput {} {
    if {[gets stdin line] >= 0} {
        puts "You entered: $line"
    }
}
fileevent stdin readable readInput
vwait forever

This sets up an event-driven input handler. The readInput procedure is called when input is available. This allows other processing while waiting.

Best Practices

This tutorial covered the Tcl gets command with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.