Description |
The Read procedure reads a single line of data from a file or the console.
Version 1
Is used to read text values from the console.
Version 2
Is used to read text values from a text file with the given FileHandle.
Version 3
Is used to read data from a binary file with the given FileHandle.
You must use AssignFile to assign a file to the FileHandle and open the file with Reset before using Read.
For text files, each line of text is parsed into the given variables. These variables may be text or number types.
For strings, the whole line is read, unless it exceeds the string variable capacity - only that amount of text that fits is passed.
When parsing for numbers, white space characters and line ends are seen as separator values. If a number value exceeds the capacity of the variable, it is cast to the variable without raising an exception.
When reading for strings or characters, a ReadLn must be performed when Eoln (end of the line) is reached. Otherwise, subsequent Read calls repeatedly return a null value.
For binary files, data values Value1, Value2 etc, are read from the data the file. If the file given by FileHandle is a typed file (one defined to contained defined records), then these values must be of the same type (record).
|
|
Notes |
You cannot use Read to read from an untyped binary file (one declared as File with no following of type).
To read from an untyped binary file, use BlockRead.
Read does not use buffering - BlockRead is more efficient.
Read is also a Delphi directive. It is used with the Property keyword.
|
|
Related commands |
AssignFile |
|
Assigns a file handle to a binary or text file |
CloseFile |
|
Closes an open file |
Eof |
|
Returns true if a file opened with Reset is at the end |
Eoln |
|
Returns true if the current text file is pointing at a line end |
File |
|
Defines a typed or untyped file |
ReadLn |
|
Read a complete line of data from a text file |
Reset |
|
Open a text file for reading, or binary file for read/write |
TextFile |
|
Declares a file type for storing lines of text |
Write |
|
Write data to a binary or text file |
WriteLn |
|
Write a complete line of data to a text file |
|
|
|
Example code : Reading single characters at a time from a text file |
var
myFile : TextFile;
letter : char;
text : string;
begin // Try to open the Test.txt file for writing to
AssignFile(myFile, 'Test.txt');
ReWrite(myFile);
// Write lines of text to the file
WriteLn(myFile, 'Hello');
WriteLn(myFile, 'To you');
// Close the file
CloseFile(myFile);
// Reopen the file for reading only
FileMode := fmOpenRead;
Reset(myFile);
// Display the file contents
while not Eof(myFile) do
begin // Proces one line at a time
WriteLn('Start of a new line :');
while not Eoln(myFile) do
begin Read(myFile, letter); // Read and display one letter at a time
ShowMessage(letter);
end;
ReadLn(myFile, text);
end;
// Close the file for the last time
CloseFile(myFile);
end;
|
Show full unit code |
Start of a new line :
H
e
l
l
o
Start of a new line :
T
o
y
o
u
|
|
Example code : Reading word data from a typed binary file |
var
myWord, myWord1, myWord2 : Word;
myFile : File of Word;
begin // Try to open the Test.cus binary file for writing to
AssignFile(myFile, 'Test.cus');
ReWrite(myFile);
// Write a couple of lines of Word data to the file
myWord1 := 234;
myWord2 := 567;
Write(myFile, myWord1, myWord2);
// Close the file
CloseFile(myFile);
// Reopen the file in read only mode
Reset(myFile);
// Display the file contents
while not Eof(myFile) do
begin
Read(myFile, myWord);
ShowMessage(IntToStr(myWord));
end;
// Close the file for the last time
CloseFile(myFile);
end;
|
Show full unit code |
234
567
|
|