Moving in Binary File

Hello

I am reading data from binary file ,some of this

data is string and some is images

and the length of some of this data is saved in many places in the file

I need a way to move back in the file

i use the Seek method with the StreamReader to do that when i read text files

but there is no Seek method for the BinaryReader

Any body can help me .



Answer this question

Moving in Binary File

  • Scooter18

    Yes

    when i use (char) BinaryReader.ReadByte

    the program work

    while when i use BinaryReader.ReadChar

    an exception is raised


  • Eric Small

    thank you

    That is what i look for


  • Ed Noepel

    Are you saying BinaryReader.ReadByte works and BinaryReader.ReadChar fails

  • Pete Kane

    I try to read the file and every thing is good

    until the BinarReader Position property  = 34024

    in this position an exception raised

    and the this message box  shown :

    ---------------------------

    ---------------------------
    The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.DecoderReplacementFallback'.

    Parameter name: chars
    ---------------------------
    OK  
    ---------------------------

    Can you tell me what is the wrong

    Thank you


  • wjrichardlee

    I try to use the BinaryReader ReadByte Method

    to read and it work so good

    Is there any different between ReadByte and ReadChar Methods for the

    BinarySteam


  • Chris Henesy

    I can't see to reproduce anything like that. Maybe you can post more detail

  • Anne8408

    When you create a BinaryReader object you give it the steam with which to use. You can use that Stream to seek backwards. If you didn't keep a reference to that stream you can use the BinaryReader.BaseStream property. Example

    int i = reader.ReadInt32();
    reader.BaseStream.Seek(-sizeof(Int32), System.IO.SeekOrigin.Current);
    i = reader.ReadInt32();



  • arafathm

    I have a binary file , this file hold data about cheques

    this data consist of char data like cheque number and binary data

    like cheque image

    I tried to read the data using ReadChar method

    like this :

    BinaryReader binReader;

    try
    {

    binReader = new BinaryReader(File.Open(args[0], FileMode.Open));

    }
    catch (FileNotFoundException exc)
    {
    MessageBox.Show(exc.Message);
    return;
    }
    catch (IndexOutOfRangeException exc)
    {
    MessageBox.Show(exc.Message + "\nUsage: ShowFile File");
    return;
    }

    do
    {
    try
    {

    // read the file
    binReader.ReadChar();

    }

    } while (binReader.BaseStream.Position !=(binReader.BaseStream.Length-1));

    but a exception is raised and its message :

    ---------------------------

    ---------------------------
    The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.DecoderReplacementFallback'.

    Parameter name: chars
    ---------------------------
    OK
    ---------------------------

    I try

    (char)binReader.ReadByte();

    istead of

    binReader.ReadChar();

    and it is work

    I want to know why this happen

    is there any different between the to methods in the way

    to read the file


  • Moving in Binary File