File I/O line-by-line (for text files) is fairly simple.
Assign associates an external filename with a file variable. This is the first step in file I/O. After the Assign, all references to the file are made through the file variable.
Reset opens a file for reading, preserving the contents and placing the file position at the beginning.
Rewrite opens a file for writing, destroying the contents and placing the file position at the beginning.
For Reset and Rewrite: If the file is an untyped file (i.e. F : file ), an optional record size parameter can be passed to the call (i.e. Reset( F, 256 )). Default record size is 128.
Append is used to open a text file, placing the file position at the end, to allow for the addition of text to the file.
File I/O with typed files is just about the same. Declare a file of a structured type (i.e. F : file of CustRec), then do a Reset. To get to record number N, call Seek( F, N ), where the first record in the file is record 0. In the case of file I/O on untyped files, Seek( F, N ) will seek N records into the file based on the filesize passed when the file was opened.
Be aware that some of this information may be specific to Delphi, but I believe it to be applicable to modern "generic" Pascal compilers. (For instance, Delphi now uses AssignFile to replace Assign, as Assign is now a method call in objects).
<strong>procedure</strong> CopyFile;
<strong>var</strong>
F1 : text;
F2 : text;
FN1 : <strong>string</strong>;
FN2 : <strong>string</strong>;
<strong>begin</strong>
FN1 := 'infile.txt';
FN2 := 'outfile.txt';
assign( F1, FN1 );
reset( F1 ); <em>{ open file for reading }</em>
assign( F2, FN2 );
rewrite( F2 ); <em>{ open file for writing, destroying contents, if any }</em>
<strong>while not</strong> EOF( F1 ) <strong>do</strong>
<strong>begin</strong>
readln( F1 );
<em>{ file manipulation goes here }</em>
writeln( F2 );
<strong>end</strong>;
close( F1 );
close( F2 );
<strong>end</strong>;