RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Read Function

Read reads data from a file.

Pascal
procedure Read(F; V1); overload;
procedure Read(F; V1; Vn); overload;
procedure Read(F: Text; V1); overload;
procedure Read(F: Text; V1; Vn); overload;
C++
Read( F,  V1);
Read( F,  V1,  Vn);
Read(Text F,  V1);
Read(Text F,  V1,  Vn);

The Read procedure can be used in Delphi code in the following ways. 

For typed files, it reads a file component into a variable. 

For text files, it reads one or more values into one or more variables. 

The syntax shown here for Read demonstrates that the procedure can take a variable number of arguments. 

Read reads all characters up to, but not including, the next end-of-line marker or until Eof(F) becomes true; it does not skip to the next line after reading. If the resulting string is longer than the maximum length of the string variable, it is truncated. 

After the first Read, each subsequent Read sees the end-of-line marker and returns a zero-length string. 

Use multiple Readln calls to read successive string values. 

When the extended syntax is enabled, Read can read null-terminated strings into zero-based character arrays. 

Read reads one character from the file and assigns it to the variable. If CRLF mode is enabled and Eof(F) was true before Read was executed, the value Chr(26) (a Ctrl-Z character) is assigned to the variable. (To enable CRLF mode, use SetLineBreakStyle.) 

Read skips any blanks, tabs, or end-of-line markers preceding the numeric string. 

If the numeric string does not conform to the expected format, an I/O error occurs; otherwise, the value is assigned to the variable. 

The next Read starts with the blank, tab, or end-of-line marker that terminated the numeric string.  

Delphi Examples: 

 

{
This example reads a file one character at a time and writes
it into a file of your chosing.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  F1, F2: TextFile;
  Ch: AnsiChar;
begin
  if OpenDialog1.Execute then begin
    AssignFile(F1, OpenDialog1.Filename);
    Reset(F1);
    if SaveDialog1.Execute then begin
      AssignFile(F2, SaveDialog1.Filename);
      Rewrite(F2);
      while not Eof(F1) do
      begin
        Read(F1, Ch);
        Write(F2, Ch);
      end;
      CloseFile(F2);
    end;
    CloseFile(F1);
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!