Vous êtes sur la page 1sur 16

TCL – File Handling

Prof. C. Prayline Rajabai


Assistant Professor
SENSE
VIT University
Overview
 File handling in Tcl is done using the built in commands

 Open

 Read

 Puts

 Gets and

 close.

2 VIT - SENSE 05-02-2019


Opening a File
 A file can be opened using the open command

 There are two arguments to the open command, filename and the

accessmode.

 Filename is a string literal, which is used to name your file.

 accessMode - can have one of the following values(r, w, a, r+, w+, a+).

Syntax :

open fileName accessMode

3 VIT - SENSE 05-02-2019


Accessmode of a file
Accessmode Description

r Opens an existing text file for reading purpose and the file must
exist. This is the default mode used when no accessMode is specified

w Opens a text file for writing, if it does not exist then a new file is
created else existing file is truncated.

a Opens a text file for writing in appending mode and file must exist.

r+ Opens a text file for reading and writing both. File must exist already

w+ Opens a text file for reading and writing both. It file exits it truncates
the file to zero length otherwise creates the new file.

a+ Opens a text file for reading and writing both. It creates the file if it
does not exist. The reading will start from the beginning but writing
can only be appended.

4 VIT - SENSE 05-02-2019


Closing a file
 To close a file, use the close command.

 Any file that has been opened by a program must be closed when the program

finishes using that file.

 Syntax : close fileName

5 VIT - SENSE 05-02-2019


Writing a File
Syntax for writing into a file :

puts $filename "text to write“

#Example program to write into a file :

set fp [open "input.txt" w+]

puts $fp "test"

close $fp

When the above code is executed it creates new file input.txt and prints test to
that file.

6 VIT - SENSE 05-02-2019


Reading a File
Syntax to read a file :

set file_data [read $fp]

Example program to read and print the contents of the file.

set fp [open "input.txt" w+]

puts $fp "test"

close $fp

set fp [open "input.txt" r]

set file_data [read $fp]

puts $file_data

close $fp

7 VIT - SENSE 05-02-2019


Reading file line by line
set fp [open "input.txt" w+]

puts $fp "test\ntest"

close $fp

set fp [open "input.txt" r]

while { [gets $fp data] >= 0 } {

puts $data

close $fp

Output

test

test
8 VIT - SENSE 05-02-2019
Random access to files
 Random access to files is done using the seek, tell and eof

commands.
 When a file is opened the access position is set to the beginning or end of the

file, depending on the access mode you specified to open.

 After each read or write operation the access position increments by the

number of bytes transferred.

 The seek command may be used to change the current access position.

 seek takes two arguments, a file identifier and an integer offset within the file.

Example :

seek $f 2000

9 VIT - SENSE 05-02-2019


Random access to files
 Seek can also take a third argument that specifies an origin for the offset.

 The third argument must be either start, current, or end.

Example :

seek $f -100 end

 The tell command returns the current access position for a particular

file identifier.
tell $f

 The eof command returns 1 if an end-of-file condition has occurred

on the file.
eof $f

10 VIT - SENSE 05-02-2019


Manipulating file names : Glob
 Tcl has two commands for manipulating file names : glob and file.

 The glob command takes one or more file name patterns as arguments and returns
a list of all the file names that match the pattern.
Example :
glob *.c *.h
⇒ main.c hash.c hash.h
glob {{src,backup}/*.[ch]}
⇒ src/main.c src/hash.c src/hash.h backup/hash.c
glob */
⇒ return a list of all the subdirectories of the current directory.
glob -type d *
⇒ return a list of all the subdirectories of the current directory.
Options -> d – directory, f – plain file, r – read, w – write

11 VIT - SENSE 05-02-2019


File information commands
Syntax :
file info-option name
 info-option specifies the information desired, such as exists or readable or
size.
 name is the name of the file.

 The exists, isfile, isdirectory, and type options return information(either 0


or 1) about the nature of a file.

 The readable, writable, and executable options return 0 or 1 based on the


permissions given to the current user.

 The owned option returns 1 if the current user is the file’s owner and 0
otherwise.

 The size option returns a decimal string giving the size of the file in bytes.

 The mtime option returns the time when the file was last modified.
12 VIT - SENSE 05-02-2019
Manipulating file names : file
Examples :

file dirname /a/b/c

/a/b

file extension src/main.c

.c

file rootname src/main.c

src/main

file tail /a/b/c

13 VIT - SENSE 05-02-2019


Exercise
 Write a program that prompts the user with the list of file names in the

directory and to enter any of the filename to be read. Pick the file from the
directory and display the contents of that file, line by line. Make sure the end
of file is handled properly, and remember to close the file after you finish.

14 VIT - SENSE 05-02-2019


Exercise
 Write a program that creates a file called “data.dat” in the current directory.

Prompt the user for five numbers, and write them, one at a time, on both the
screen and into the file. Close the file, then open it again for reading only,
and display the contents on the screen.

15 VIT - SENSE 05-02-2019


Thank You…

Vous aimerez peut-être aussi