Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

BinaryReader.PeekChar Method

Returns the next available character and does not advance the byte or character position.

[Visual Basic]
Public Overridable Function PeekChar() As Integer
[C#]
public virtual int PeekChar();
[C++]
public: virtual int PeekChar();
[JScript]
public function PeekChar() : int;

Return Value

The next available character, or -1 if no more characters are available or the stream does not support seeking.

Exceptions

Exception Type Condition
IOException An I/O error occurs.

Remarks

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for the BinaryReader class.

[Visual Basic] 
Dim binReader As New BinaryReader( _
    File.Open(fileName, FileMode.Open))
Try

    ' If the file is not empty, 
    ' read the application settings.
    If binReader.PeekChar() <> -1 Then
        aspRatio   = binReader.ReadSingle()
        lkupDir     = binReader.ReadString()
        saveTime  = binReader.ReadInt32()
        statusBar = binReader.ReadBoolean()
        Return
    End If

' If the end of the stream is reached before reading
' the four data values, ignore the error and use the
' default settings for the remaining values.
Catch ex As EndOfStreamException
    Console.WriteLine("{0} caught and ignored. " & _ 
        "Using default values.", ex.GetType().Name)
Finally
    binReader.Close()
End Try

[C#] 
BinaryReader binReader = 
    new BinaryReader(File.Open(fileName, FileMode.Open));
try
{
    // If the file is not empty, 
    // read the application settings.
    if(binReader.PeekChar() != -1)
    {
        aspectRatio   = binReader.ReadSingle();
        lookupDir     = binReader.ReadString();
        autoSaveTime  = binReader.ReadInt32();
        showStatusBar = binReader.ReadBoolean();
    }
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException e)
{
    Console.WriteLine("{0} caught and ignored. " + 
        "Using default values.", e.GetType().Name);
}
finally
{
    binReader.Close();
}

[C++] 
BinaryReader* binReader = 
    new BinaryReader(File::Open(fileName, FileMode::Open));
try
{
    // If the file is not empty, 
    // read the application settings.
    if(binReader->PeekChar() != -1)
    {
        aspectRatio   = binReader->ReadSingle();
        lookupDir     = binReader->ReadString();
        autoSaveTime  = binReader->ReadInt32();
        showStatusBar = binReader->ReadBoolean();
        return;
    }
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException* e)
{
    Console::WriteLine(S"{0} caught and ignored. "
        S"Using default values.", e->GetType()->Name);
}
__finally
{
    binReader->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

BinaryReader Class | BinaryReader Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft