Home  |  Delphi .net Home  |  System.IO.StreamReader  |  Read Method
  
Read  
Method  
Reads the next character or block of characters from the current stream
StreamReader Class
System.IO NameSpace
CF1.  Function Read : Char;
CF2.  Function Read ( Characters : Array of Char; StartIndex : Integer; Count : Integer ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
If the current stream has not been exhausted, the next character, or block of characters are returned.
 
The stream position is updated accordingly.
 
In the first syntax, the Read method returns the next stream character is returned as an integer. The integer value is the numeric equivalent of the character, or -1 if the stream was exhausted. Carriage return and Line-feed characters are returned by this method.
 
In the second syntax, an attempt is made to read up to Count characters. These characters are overlaid onto the Characters array from StartIndex. The returned integer value is the number of characters overlaid. Zero if the stream was already exhausted before the read. Unlike StringReader, in this 2nd syntax, Carriage return and Line-feed characters are returned by this syntax.
Notes
You must set the length of the target Character array in the second syntax, and it must have sufficient capacity to hold the returned characters.
Microsoft MSDN Links
System.IO
System.IO.StreamReader
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Reading a single character at a time
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Reader        : System.IO.StreamReader;
  Writer        : System.IO.StreamWriter;
  CharAsInteger : Integer;

begin
  // Create our Stream Writer to write to a text file
  Writer := System.IO.StreamWriter.Create('C:\DelphiBasics.txt');

  // Write various things to our StreamWriter
  Writer.WriteLine('Hello');
  Writer.WriteLine('World');

  // Close the stream
  Writer.Close;

  // Read the stream
  Reader := System.IO.StreamReader.Create('C:\DelphiBasics.txt');

  while Reader.Peek >= 0 do
  begin
    CharAsInteger := Reader.Read;

    if CharAsInteger = 13  // Carriage Return
    then Console.Write('[CR]')
    else if CharAsInteger = 10  // Line-feed
    then Console.Write('[LF]')
    else Console.Write(Char(CharAsInteger));
  end;

  Console.Readline;
end.
   Hello[CR][LF]World[CR][LF]
Reading blocks of characters
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Reader        : System.IO.StreamReader;
  Writer        : System.IO.StreamWriter;
  CharArray     : Array of Char;
  CharCount     : Integer;
  CharAsInteger : Integer;

begin
  // Create our Stream Writer to write to a text file
  Writer := System.IO.StreamWriter.Create('C:\DelphiBasics.txt');

  // Write various things to our StreamWriter
  Writer.WriteLine('Hello');
  Writer.WriteLine('World');

  // Close the stream
  Writer.Close;

  // Read the stream
  Reader := System.IO.StreamReader.Create('C:\DelphiBasics.txt');

  // Create our target character array
  SetLength(CharArray, 4);

  // Read the characters from the stream in blocks
  CharCount := Reader.Read(CharArray, 0, 4);
  while CharCount > 0 do
  begin
    Console.WriteLine(CharCount.ToString +
                      ' chars read : '   +
                      CharArray);

    // Clear the char array before assigning into it
    CharArray[0] := ' ';
    CharArray[1] := ' ';
    CharArray[2] := ' ';
    CharArray[3] := ' ';
    CharCount := Reader.Read(CharArray, 0, 4);
  end;

  Console.Readline;
end.
   4 chars read : Hell
   4 chars read : o
   W                      // Includes line feed characters
   4 chars read : orld
   2 chars read :         // Line feed characters
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author