File input/output: Difference between revisions
Content added Content deleted
(Clarified task description so as to encourage didactic examples and discourage uninformative tricks.) |
m (→{{header|Tcl}}: more polish) |
||
Line 1,710: | Line 1,710: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|tclsh}} |
{{works with|tclsh}} |
||
{{works with|eTcl}} |
{{works with|eTcl}} |
||
{{works with|wish}} |
{{works with|wish}} |
||
{{works with|tixwish}} |
{{works with|tixwish}} |
||
{{works with|tclkit}} |
|||
<lang tcl>set in [open "input.txt" r] |
<lang tcl>set in [open "input.txt" r] |
||
set out [open "output.txt" w] |
set out [open "output.txt" w] |
||
Line 1,723: | Line 1,720: | ||
close $in |
close $in |
||
close $out</lang> |
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 |
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 — or 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] |
<lang tcl>set in [open "input.txt" r] |
||
set out [open "output.txt" w] |
set out [open "output.txt" w] |
||
Line 1,729: | Line 1,726: | ||
close $in |
close $in |
||
close $out</lang> |
close $out</lang> |
||
Or the minimal version if we don't need any processing of the data at all: |
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> |
<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 |
<lang tcl>#open file for writing |
||
set myfile [open "README.TXT" w] |
set myfile [open "README.TXT" w] |
||
Line 1,741: | Line 1,736: | ||
#close the file |
#close the file |
||
close $myfile</lang> |
close $myfile</lang> |
||
;Reading a line from a file<nowiki>:</nowiki> |
|||
<lang tcl>#open file for reading |
<lang tcl>#open file for reading |
||
set myfile [open "README.TXT" r] |
set myfile [open "README.TXT" r] |