File input/output: Difference between revisions

no edit summary
No edit summary
Line 51:
</Ada>
The following solution uses stream I/O. Any file of Ada.Text_IO can be used to obtain a corresponding stream. Reading and writing streams is more efficient than reading text files directly, because it skips formatting. Note also how End_Error exception is used to avoid End_Of_File. End_Of_File is depreciated as it requires file look-ahead, and thus is much less efficient.
<lang ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;
Line 68:
Close (Outfile);
end File_IO;
</adalang>
=={{header|ALGOL 68}}==
PROC copy file = (STRING in name, out name)VOID: (
Line 146:
A couple of remarks on the preceding example:
 
It uses <codett>fgetc</codett> to read one character at a time. Each character is visited, even though there's nothing to do with it. Copying bigger blocks of data is much more efficient.
 
It uses buffered IO to perform the move, which is overkill. This is not actually a weakness, but it invokes some overhead that you don't need.
Line 247:
=={{header|C++}}==
{{works with|g++|3.4.2}}
<lang cpp>#include <iostream>
#include <fstream>
#include <string>
Line 275:
}
return 0;
}</cpplang>
 
Simpler version:
 
<lang cpp>#include <iostream>
#include <fstream>
#include <cstdlib>
Line 307:
return EXIT_SUCCESS;
}</cpplang>
 
Using istream- and ostream- iterators:
 
<lang cpp># include <algorithm>
# include <fstream>
 
Line 320:
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(ofile));
}</cpplang>
 
Even simpler way:
 
<lang cpp>#include <fstream>
 
int main()
Line 331:
std::ofstream output("output.txt");
output << input.rdbuf();
}</cpplang>
 
=={{header|Clean}}==
Line 392:
do (write-sequence buffer out :end size))))
 
If you're on an odd platform which actually stores text/binary/... type information for files and your CL implementation will use this information, then <codett>in</codett> should be opened with <codett>:element-type :default</codett>.
 
=={{header|Delphi}}==
Line 700:
=={{header|OCaml}}==
By line:
<lang ocaml>let () =
let ic = open_in "input.txt" in
let oc = open_out "output.txt" in
Line 712:
close_in ic;
close_out oc;
;;</ocamllang>
 
By character:
<lang ocaml>let () =
let ic = open_in "input.txt" in
let oc = open_out "output.txt" in
Line 726:
close_in ic;
close_out oc;
;;</ocamllang>
 
(Notice that ic and oc, of type ''in_channel'' and ''out_channel'', are buffered)
Line 811:
The following use of the standard libraries shutil.copyfile is to be preferred. (Current source code ensures that failure to open files raises appropriate exceptions, a restricted buffer is used to copy the files using binary mode, and any used file descriptors are always closed).
 
<lang python>import shutil
shutil.copyfile('input.txt', 'output.txt')</pythonlang>
 
However the following example shows how one would do file I/O of other sorts:
 
<lang python>
infile = open('input.txt', 'r')
outfile = open('output.txt', 'w')
Line 823:
outfile.close()
infile.close()
</pythonlang>
 
This does no error checking. A more robust program would wrap each open with exception handling blocks:
 
<lang python>
import sys
try:
Line 848:
infile.close()
outfile.close()
</pythonlang>
 
In Python 2.6 (or 2.5 if we use ''from __future__ import with_statement'') we can more simply write:
 
<lang python>
import sys
try:
Line 863:
sys.exit(1)
 
</pythonlang>
 
The files will automatically be closed on exit of their ''with:'' blocks. (Thus even if an I/O error occurred while reading the middle of the input file we are assured that the ''.close()'' method will have been called on each of the two files.