Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# checking for binary reader end of file

I was searching for a way to check whether I've reached the end of a file for my binary reader and one suggestion was to use PeekChar as such

while (inFile.PeekChar() > 0) {     ... } 

However, it looks like I've run into an issue

 Unhandled Exception: System.ArgumentException: The output char buffer is too sma ll to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'Syste m.Text.DecoderReplacementFallback'. Parameter name: chars    at System.Text.Encoding.ThrowCharsOverflow()    at System.Text.Encoding.ThrowCharsOverflow(DecoderNLS decoder, Boolean nothin gDecoded)    at System.Text.UTF8Encoding.GetChars(Byte* bytes, Int32 byteCount, Char* char s, Int32 charCount, DecoderNLS baseDecoder)    at System.Text.DecoderNLS.GetChars(Byte* bytes, Int32 byteCount, Char* chars,  Int32 charCount, Boolean flush)    at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteC ount, Char[] chars, Int32 charIndex, Boolean flush)    at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteC ount, Char[] chars, Int32 charIndex)    at System.IO.BinaryReader.InternalReadOneChar()    at System.IO.BinaryReader.PeekChar() 

So maybe PeekChar isn't the best way to do it, and I don't think it should even be used that way because I'm checking the current position of my reader and not really what the next character is supposed to be.

like image 839
MxLDevs Avatar asked Jun 08 '12 03:06

MxLDevs


People also ask

What C is used for?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

There is a more accurate way to check for EOF when working with binary data. It avoids all of the encoding issues that come with the PeekChar approach and does exactly what is needed: to check whether the position of the reader is at the end of the file or not.

while (inFile.BaseStream.Position != inFile.BaseStream.Length) {    ... } 
like image 130
MxLDevs Avatar answered Sep 18 '22 15:09

MxLDevs


Wrapping it into a Custom Extension Method that'll extend the BinaryReader class by adding the missing EOF method.

public static class StreamEOF {      public static bool EOF( this BinaryReader binaryReader ) {         var bs = binaryReader.BaseStream;         return ( bs.Position == bs.Length);     } } 

So now you can just write:

while (!infile.EOF()) {    // Read.... } 

:) ... assuming you have created infile somewhere like this:

var infile= new BinaryReader(); 

Note: var is implicit typing. Happy to found it - it's other puzzle piece for well styled code in C#. :D

like image 29
Un Peu Avatar answered Sep 20 '22 15:09

Un Peu