Sie sind auf Seite 1von 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

Das könnte Ihnen auch gefallen