File input/output: Difference between revisions

Content added Content deleted
(→‎{{header|Tcl}}: show how to do it with fcopy, add formatting)
Line 999: Line 999:
{{works with|tixwish}}
{{works with|tixwish}}


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]
# Obviously, arbitrary transformations could be added to the data at this point
puts -nonewline $out [read $in]
puts -nonewline $out [read $in]
close $in
close $out
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 not shown here):
<lang tcl>set in [open "input.txt" r]
set out [open "output.txt" w]
fcopy $in $out
close $in
close $out</lang>


or the minimal version if we don't need any processing of the data
Or the minimal version if we don't need any processing of the data at all:
file copy input.txt output.txt
<lang tcl>file copy input.txt output.txt</lang>


Other File I/O:
Other File I/O:


#open file for writing
<lang tcl>#open file for writing
set myfile [open "README.TXT" w]
set myfile [open "README.TXT" w]
#write something to the file
#write something to the file
puts $myfile "This is line 1, so hello world...."
puts $myfile "This is line 1, so hello world...."
#close the file
#close the file
close $myfile
close $myfile</lang>


<lang tcl>#open file for reading

set myfile [open "README.TXT" r]
#open file for reading
#read something from the file
set myfile [open "README.TXT" r]
gets $myfile mydata
#read something from the file
#show what was read from the file
gets $myfile mydata
#should print "This is line1, so hello world...."
#show what was read from the file
puts $mydata
#should print "This is line1, so hello world...."
#close the file
puts $mydata
#close the file
close $myfile</lang>
close $myfile


=={{header|Toka}}==
=={{header|Toka}}==