0% found this document useful (0 votes)
70 views4 pages

TCL File I/O: Opening Files

Tcl File i_o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views4 pages

TCL File I/O: Opening Files

Tcl File i_o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

9/23/2016

TclFileI/O

TclFileI/O
Advertisements

PreviousPage

NextPage

Tclsupportsfilehandlingwiththehelpofthebuiltincommandsopen,read,puts,gets,and
close.
Afilerepresentsasequenceofbytes,doesnotmatterifitisatextfileorbinaryfile.

OpeningFiles
TclusestheopencommandtoopenfilesinTcl.Thesyntaxforopeningafileisasfollows
openfileNameaccessMode

Here,filenameisstringliteral,whichyouwillusetonameyourfileandaccessMode can
haveoneofthefollowingvalues
S.No.
1

Mode&Description

r
Opensanexistingtextfileforreadingpurposeandthefilemustexist.Thisisthe
defaultmodeusedwhennoaccessModeisspecified.

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

a
Opens a text file for writing in appending mode and file must exist. Here, your
programwillstartappendingcontentintheexistingfilecontent.

https://www.tutorialspoint.com/tcltk/tcl_file_io.htm

1/4

9/23/2016

TclFileI/O

r+
Opensatextfileforreadingandwritingboth.Filemustexistalready.

w+
Opens a text file for reading and writing both. It first truncate the file to zero
lengthifitexistsotherwisecreatethefileifitdoesnotexist.

a+
Opens a text file for reading and writing both. It creates the file if it does not
exist.Thereadingwillstartfromthebeginning,butwritingcanonlybeappended.

ClosingaFile
Tocloseafile,usetheclosecommand.Thesyntaxforcloseisasfollows
closefileName

Anyfilethathasbeenopenedbyaprogrammustbeclosedwhentheprogramfinishesusing
thatfile.Inmostcases,thefilesneednotbeclosedexplicitlytheyareclosedautomatically
whenFileobjectsareterminatedautomatically.

WritingaFile
Putscommandisusedtowritetoanopenfile.
puts$filename"texttowrite"

Asimpleexampleforwritingtoafileisshownbelow.
#!/usr/bin/tclsh
setfp[open"input.txt"w+]
puts$fp"test"
close$fp

When the above code is compiled and executed, it creates a new file input.txt in the
directorythatithasbeenstartedunder(intheprogram'sworkingdirectory).

ReadingaFile
https://www.tutorialspoint.com/tcltk/tcl_file_io.htm

2/4

9/23/2016

TclFileI/O

Followingisthesimplecommandtoreadfromafile
setfile_data[read$fp]

Acompleteexampleofreadandwriteisshownbelow
#!/usr/bin/tclsh
setfp[open"input.txt"w+]
puts$fp"test"
close$fp
setfp[open"input.txt"r]
setfile_data[read$fp]
puts$file_data
close$fp

Whentheabovecodeiscompiledandexecuted,itreadsthefilecreatedinprevioussection
andproducesthefollowingresult
test

Hereisanotherexampleforreadingfiletillendoffilelinebyline
#!/usr/bin/tclsh
setfp[open"input.txt"w+]
puts$fp"test\ntest"
close$fp
setfp[open"input.txt"r]
while{[gets$fpdata]>=0}{
puts$data
}
close$fp

Whentheabovecodeiscompiledandexecuted,itreadsthefilecreatedinprevioussection
andproducesthefollowingresult
test
test

PreviousPage

NextPage
Advertisements

https://www.tutorialspoint.com/tcltk/tcl_file_io.htm

3/4

9/23/2016

TclFileI/O

Write for us

FAQ's

Helping

Contact

Copyright 2016. All Rights Reserved.


Enter email for newsletter

https://www.tutorialspoint.com/tcltk/tcl_file_io.htm

go

4/4

You might also like