Académique Documents
Professionnel Documents
Culture Documents
Open
Read
Puts
Gets and
close.
There are two arguments to the open command, filename and the
accessmode.
accessMode - can have one of the following values(r, w, a, r+, w+, a+).
Syntax :
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.
Any file that has been opened by a program must be closed when the program
close $fp
When the above code is executed it creates new file input.txt and prints test to
that file.
close $fp
puts $file_data
close $fp
close $fp
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
After each read or write operation the access position increments by the
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
Example :
The tell command returns the current access position for a particular
file identifier.
tell $f
on the file.
eof $f
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
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 :
/a/b
.c
src/main
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.
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.