File input/output: Difference between revisions

no edit summary
(add E example)
No edit summary
Line 280:
<cffile action="write" file="#expandPath('output.txt')#" output="#inputContents#">
</cfif>
 
==[[Delphi]]==
[[Category:Delphi]]
 
Delphi supports both typed and untyped as well as a textfile type for files. Delphi provides a default 128 byte buffer for text files. This may be enlarged via a call to SetTextBuff(Var F: Text; Var Buf [Size : integer]) procedure. All other files have no buffer at all and it is the programmers option to do buffering.
 
The following file I/O procedures have existed since Turbo Pascal V-3.
 
- Read(F,V1..Vn)
- ReadLn(F,V1..Vn)
- Write(F,V1[,V2..Vn])
- WriteLn(f,V1[,V2..Vn])
- BlockRead(F,Buff,BytesToRead[,BytesRead])
- BlockWrite(F,Buff,BytesToRead[,BytesWritten])
 
Files are opened using:
 
AssignFile(f,{fully qualified path and file name})
 
-------
 
Assigns the file name to the file structure in preparation for opening.
 
Reset(f)
 
Opens and existing file. If it does not exist EIOError is raised.
 
-------
 
Rewrite(f)
 
Creates a new file and opens it for I/O. If the files exists is is overwritten.
 
 
Delphi implemented Streams of which a varient is TFileStream and are vry closely related to the Windows API for file handling.
 
'''- Text File I/O -'''
 
<pre>
var
f : TextFile ;
s : string ;
begin
AssignFile(f,[fully qualified file name);
Reset(f);
writeln(f,s);
Reset(f);
ReadLn(F,S);
CloseFile(
end;
</pre>
 
 
'''- Untyped File I/O -'''
 
This is perhaps one of the most powerfull I/O functions built into Pascal. This will allow you to open and read a file of ANY type, regardless of structure, size or content. Note the usage of Reset(). This is using the optional size paramter that instructs the record size of file I/O. This could have been called with SizeOf(Buff) as the optional parameter but that would have limited flexibility. Calling it with a size of ONE byte allows you to adjust the buffer size on the fly, as conditions warrant. Also note the use of the BytesRead parameter. When included in the BlockRead() function it will return the number of bytes actualy read. If this is not included, then if your directive to read n bytes is greater then the size of the file, the EOF will be encountered unexpectedly and EIOError will be reaised.
 
<pre>
var
f : File ;
buff : array[1.1024] of byte ;
BytesRead : Integer ;
begin
AssignFile(f,fully qualified file name);
Reset(f,1);
Blockread(f,Buff,SizeOf(Buff),BytesRead);
CloseFile(f);
end;
</pre>
 
 
'''- Typed File I/O -'''
 
Typed file I/O is very useful when reading and writing structures. An Address List is quiet easy to write when using this type of I/O. The same file procedures are used with some subtle differences. Bite below in the blockread and blockwrite procedures that the bytes to read or write are 1. Also note that the reset procedure is not called with a buffer size. When performing '''Typed File I/O''' the size of the type definition is the buffer size. In the BlockRead() and BlockWrite() procedures I elected to read '''one record'''. Had I declared a very large buffer of type tAddressBook of say 500 records, I could have set bytes to read as SizeOf(Buffer) thereby reading a minimum of 500 records.
 
<pre>
 
type
 
tAddressBook = Record
FName : string[20];
LName : string[30];
Address : string[30];
City : string[30];
State : string[2];
Zip5 : string[5];
Zip4 : string[4];
Phone : string[14];
Deleted : boolean ;
end;
 
var
f : file of tAddressBook ;
v : tAddressBook ;
bytes : integer ;
begin
AssignFile(f,fully qualified file name);
Reset(f);
Blockread(f,V,1,Bytes);
Edit(v);
Seek(F,FilePos(f)-1);
BlockWrite(f,v,1,bytes);
CloseFile(f);
end;
</pre>
 
==[[E]]==
Anonymous user