.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Moving in Binary File
 

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

Ineed 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 .

Tammam_Koujan
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();

Peter Ritchie
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();

Peter Ritchie

thank you

That is what i look for

Tammam_Koujan

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

Tammam_Koujan

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 ?

Tammam_Koujan
Are you saying BinaryReader.ReadByte works and BinaryReader.ReadChar fails?
Peter Ritchie

Yes

when i use (char) BinaryReader.ReadByte

the program work

while when i use BinaryReader.ReadChar

an exception is raised

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

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 toread 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 andits 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

Tammam_Koujan

You can use google to search for other answers

Custom Search

More Threads

• c# serial port enumeration
• Recovery from the Memory Dump
• File.GetCreationTime(path) returning odd values
• SqlBulkCopy vs XML to stored procedure
• #if false use in C#
• Include files in Class Library in a Web Service?
• Are keys and IVs of SymmetricAlgorithms safe?
• What is .NET Framework
• Key messages in WndProc when form is minimized?
• CheckedListBox datasource