How do I know current offset of BinaryReader in C#?
Asked Answered
W

2

9

I have source below:

public static void DisplayValues()
{
    float aspectRatio;
    string tempDirectory;
    int autoSaveTime;
    bool showStatusBar;

    if (File.Exists(fileName))
    {
        using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
        {
            aspectRatio = reader.ReadSingle();
            tempDirectory = reader.ReadString();
    ------------------------------------------------> I want to know current offset.
            autoSaveTime = reader.ReadInt32();
            showStatusBar = reader.ReadBoolean();
        }

        Console.WriteLine("Aspect ratio set to: " + aspectRatio);
        Console.WriteLine("Temp directory is: " + tempDirectory);
        Console.WriteLine("Auto save time set to: " + autoSaveTime);
        Console.WriteLine("Show status bar: " + showStatusBar);
    }
}

I have to find out current offset of BinaryReader.

Weslee answered 23/2, 2014 at 13:48 Comment(1)
Do note that you make the strong assumption that BinaryReader doesn't buffer bytes from the base stream. It does buffer bytes. But not in a way that will byte you.Archean
K
13

You can obtain the underlying stream by

var stream = reader.BaseStream;

and get the position by

stream.Position
Koumis answered 23/2, 2014 at 13:52 Comment(0)
C
1
BinaryReader br=null;
/ * init, read, ...*/
long pos=br.BaseStream.Position;
Constantin answered 23/2, 2014 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.