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
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
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.
   Hello[CR][LF]World[CR][LF]
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author