Jump to content

File input/output: Difference between revisions

m
(Clarified task description so as to encourage didactic examples and discourage uninformative tricks.)
m (→‎{{header|Tcl}}: more polish)
Line 1,710:
=={{header|Tcl}}==
{{works with|tclsh}}
 
{{works with|eTcl}}
 
{{works with|wish}}
 
{{works with|tixwish}}
{{works with|tclkit}}
 
<lang tcl>set in [open "input.txt" r]
set out [open "output.txt" w]
Line 1,723 ⟶ 1,720:
close $in
close $out</lang>
For larger files, it is better to use the <tt>fcopy</tt> command, though in general this restricts what operations can be performed rather more (only encoding and end-of-line translations are possible, though notor more general byte-level transformations with the generic filter mechanism provided in Tcl 8.6 — none of which are shown here):
<lang tcl>set in [open "input.txt" r]
set out [open "output.txt" w]
Line 1,729 ⟶ 1,726:
close $in
close $out</lang>
 
Or the minimal version if we don't need any processing of the data at all:
<lang tcl>file copy input.txt output.txt</lang>
===Other key file I/O operations===
 
;Writing a line to a file<nowiki>:</nowiki>
Other File I/O:
 
<lang tcl>#open file for writing
set myfile [open "README.TXT" w]
Line 1,741 ⟶ 1,736:
#close the file
close $myfile</lang>
;Reading a line from a file<nowiki>:</nowiki>
 
<lang tcl>#open file for reading
set myfile [open "README.TXT" r]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.