Home  |  Delphi .net Home  |  System.IO.StreamReader  |  Peek Method
Peek  
Method  
Gives the value of the next character in the stream without updating position
StreamReader Class
System.IO NameSpace
CF1.  Function Peek ( ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
The integer value of the next character in the current stream is returned, but the position is not moved to the next character. The character is literally 'peeked'.
 
If the stream has already been exhausted, -1 is returned.
Microsoft MSDN Links
System.IO
System.IO.StreamReader
 
 
Stopping stream read before a certain character is read
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.
Show full unit code
  Hello[CR][LF]World[CR][LF]
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author