- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Input & output
In this chapter, we will work with input and output operations in Tcl. Tcl has several commands for doing io. We will cover a few of them.
Tcl uses objects called channels to read and write data. The channels can be created using the open
or socket
command. There are three standard channels available to Tcl scripts without explicitly creating them. They are automatically opened by the OS for each new application. They are stdin
, stdout
and stderr
. The standard input, stdin
, is used by the scripts to read data. The standard output, stdout
, is used by scripts to write data. The standard error, stderr
, is used by scripts to write error messages.
In the first example, we will work with the puts
command. It has the following synopsis:
puts ?-nonewline? ?channelId? string
The channelId is the channel where we want to write text. The channelId is optional. If not specified, the default stdout
is assumed.
#!/usr/bin/tclsh puts "Message 1" puts stdout "Message 2" puts stderr "Message 3"
The puts
command writes text to the channel.
puts "Message 1"
If we do not specify the channelId, we write to stdout
by default.
puts stdout "Message 2"
This line does the same thing as the previous one. We only have explicitly specified the channelId.
puts stderr "Message 3"
We write to the standard error channel. The error messages go to the terminal by default.
$ ./printing.tcl Message 1 Message 2 Message 3
Example output.
The read command
The read
command is used to read data from a channel. The optional argument specifies the number of characters to read. If omitted, the command reads all of the data from the channel up to the end.
#!/usr/bin/tclsh set c [read stdin 1] while {$c != "q"} { puts -nonewline "$c" set c [read stdin 1] }
The script reads a character from the standard input channel and then writes it to the standard output until it encounters the q character.
set c [read stdin 1]
We read one character from the standard input channel (stdin).
while {$c != "q"} {
We continue reading characters until the q is pressed.
The gets command
The gets
command reads the next line from the channel, returns everything in the line up to (but not including) the end-of-line character.
#!/usr/bin/tclsh puts -nonewline "Enter your name: " flush stdout set name [gets stdin] puts "Hello $name"
The script asks for input from the user and then prints a message.
puts -nonewline "Enter your name: "
The puts
command is used to print messages to the terminal. The -nonewline
option supresses the new line character.
flush stdout
Tcl buffers output internally, so characters written with puts
may not appear immediately on the output file or device. The flush
command forces the output to appear immediately.
set name [gets stdin]
The gets
command reads a line from a channel.
$ ./hello.tcl Enter your name: Jan Hello Jan
Sample output of the script.
The pwd and cd commands
Tcl has pwd
and cd
commands, similar to shell commands. The pwd
command returns the current working directory and the cd
command is used to change the working directory.
#!/usr/bin/tclsh set dir [pwd] puts $dir cd .. set dir [pwd] puts $dir
In this script, we will print the current working directory. Then we change the working directory and print the working directory again.
set dir [pwd]
The pwd
command returns the current working directory.
cd ..
We change the working directory to the parent of the current directory. We use the cd
command.
$ ./cwd.tcl /home/janbodnar/prog/tcl/io /home/janbodnar/prog/tcl
Sample output.
The glob command
Tcl has a glob
command which returns the names of the files that match a pattern.
#!/usr/bin/tclsh set files [glob *.tcl] foreach file $files { puts $file }
The script prints all files with the .tcl
extension to the console.
set files [glob *.tcl]
The glob
command returns a list of files that match the *.tcl
pattern.
foreach file $files { puts $file }
We go through the list of files and print each item of the list to the console.
$ ./globcmd.tcl attributes.tcl allfiles.tcl printing.tcl hello.tcl read.tcl files.tcl globcmd.tcl write2file.tcl cwd.tcl readfile.tcl isfile.tcl addnumbers.tcl
This is a sample output of the globcmd.tcl
script.
Working with files
The file
command manipulates file names and attributes. It has plenty of options.
#!/usr/bin/tclsh puts [file volumes] [file mkdir new]
The script prints the system's mounted volues and creates a new directory.
puts [file volumes]
The file volumes
command returns the absolute paths to the volumes mounted on the system.
[file mkdir new]
The file mkdir
creates a directory called new
.
$ ./voldir.tcl / $ ls -d */ doc/ new/ tmp/
On a Linux system, there is one mounted volume—the root directory. The ls
command confirms the creation of the new
directory.
In the following code example, we are going to check if a file name is a regular file or a directory.
#!/usr/bin/tclsh set files [glob *] foreach fl $files { if {[file isfile $fl]} { puts "$fl is a file" } elseif { [file isdirectory $fl]} { puts "$fl is a directory" } }
We go through all file names in the current working directory and print whether it is a file or a directory.
set files [glob *]
Using the glob
command we create a list of file and directory names of a current directory.
if {[file isfile $fl]} {
We execute the body of the if
command if the file name in question is a file.
} elseif { [file isdirectory $fl]} {
The file isdirectory
command determines, whether a file name is a directory. Note that on Unix, a directory is a special case of a file.
The puts
command can be used to write to files.
#!/usr/bin/tclsh set fp [open days w] set days {Monday Tuesday Wednesday Thursday Friday Saturday Sunday} puts $fp $days close $fp
In the script, we open a file for writing. We write days of a week to a file.
set fp [open days w]
We open a file named days
for writing. The open
command returns a channel id.
set days {Monday Tuesday Wednesday Thursday Friday Saturday Sunday}
This data is going to be written to the file.
puts $fp $days
We used the channel id returned by the open
command to write to the file.
close $fp
We close the opened channel.
$ ./write2file.tcl $ cat days Monday Tuesday Wednesday Thursday Friday Saturday Sunday
We run the script and check the contents of the days file.
In the following script, we are going to read data from a file.
$ cat languages Python Tcl Visual Basic Perl Java C C# Ruby Scheme
We have a simple file called languages in a directory.
#!/usr/bin/tclsh set fp [open languages r] set data [read $fp] puts -nonewline $data close $fp
We read data from the supplied file, read its contents and print the data to the terminal.
set fp [open languages r]
We create a channel by opening the languages file in a read-only mode.
set data [read $fp]
If we do not provide a second parameter to the read
command, it reads all data from the file until the end of the file.
puts -nonewline $data
We print the data to the console.
$ ./readfile.tcl Python Tcl Visual Basic Perl Java C C# Ruby Scheme
Sample run of the readfile.tcl
script.
The eof
command checks for end-of-line of a supplied channel.
#!/usr/bin/tclsh set fp [open languages] while {![eof $fp]} { puts [gets $fp] } close $fp
We use the eof
command to read the contents of a file.
while {![eof $fp]} { puts [gets $fp] }
The loop continues until the eof
returns true if it encounters the end of a file. Inside the body, we use the gets
command to read a line from the file.
$ ./readfile2.tcl Python Tcl Visual Basic Perl Java C C# Ruby Scheme
Sample run of the readfile2.tcl
script.
The next script performs some additional file operations.
#!/usr/bin/tclsh set fp [open newfile w] puts $fp "this is new file" flush $fp file copy newfile newfile2 file delete newfile close $fp
We open a file and write some text to it. The file is copied. The original file is then deleted.
file copy newfile newfile2
The file copy
command copies a file.
file delete newfile
The original file is deleted with the file delete
command.
In the final example, we will work with file attributes.
#!/usr/bin/tclsh set files [glob *] set mx 0 foreach fl $files { set len [string length $fl] if { $len > $mx} { set mx $len } } set fstr "%-$mx\s %-s" puts [format $fstr Name Size] set fstr "%-$mx\s %d bytes" foreach fl $files { set size [file size $fl] puts [format $fstr $fl $size] }
The script creates two columns. In the first column, we have the name of the file. In the second column, we display the size of the file.
foreach fl $files { set len [string length $fl] if { $len > $mx} { set mx $len } }
In this loop, we find out the most lengthy file name. This will be used when formatting the output columns.
set fstr "%-$mx\s %-s" puts [format $fstr Name Size]
Here we print the headers of the columns. To format the data, we use the format
command.
set fstr "%-$mx\s %d bytes" foreach fl $files { set size [file size $fl] puts [format $fstr $fl $size] }
We go through the list of files and print each file name and its size. The file size
command determines the size of the file.
$ ./attributes.tcl Name Size attributes.tcl 337 bytes newfile2 17 bytes allfiles.tcl 75 bytes printing.tcl 83 bytes languages 51 bytes hello.tcl 109 bytes days 57 bytes read.tcl 113 bytes files.tcl 140 bytes globcmd.tcl 82 bytes write2file.tcl 134 bytes doc 4096 bytes cwd.tcl 76 bytes tmp 4096 bytes readfile.tcl 98 bytes isfile.tcl 219 bytes
Sample run.
In this chapter, we have covered Input/Output operations in Tcl.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论