none
Closest functionality to fread using C# and .NET

    Question

  • Consider the following code snip:

     

    check_sum = 0;

    while (!feof(ifp))

    {

    fread(&btr_contents, sizeof(long int), 1, ifp);

    if (!feof(ifp))

    check_sum += ((btr_contents & 0xffff0000) >> 16) +

    (btr_contents & 0x0000ffff);

    }/*End while: Not at end of file */

    check_sum &= 0x0000ffff;

     

     

    I need .NET equivalence to this.  What is the closest that is used to replicate the functionality of fread?  The code is used to calculate checksums on a binary files.  I've seen so many different examples using the Stream class as well as using BinaryReader and want to keep it as simple as the legacy code illustrates.

     

    Thanks in advance folks.

     

    -Al

    Wednesday, October 10, 2007 12:16 PM

Answers

  • There are many different ways to do what you want.  Your code streams the file one long int at a time.  For each value you update the checksum.  To do that in .NET you'd do the same thing.  Which class you actually use to stream/read the file doesn't really matter but I'm partial to BinaryReader in this case since you want to read bytes.  The Stream class is the underlying implementation but you gain nothing by using it directly.

     

    using(BinaryReader rdr = new BinaryReader(File.OpenReade(<path>)))
    {

       while (rdr.BaseStream.Position < rdr.BaseStream.Length)
       {

          ulong value = rdr.ReadUInt64();

          //Do work

       };
    };

     

    BinaryReader (nor the underlying Stream) doesn't expose an end of file property like StreamReader does.  Don't ask me why.  Therefore you'll need to compare the position to the length.

     

    Note that if the file isn't that big then you could also use File.ReadAllBytes to load the entire file into a byte array where you can process it faster.  This only works for small files though.

     

    Also be aware that most of the time you would create a stream decorator that you can then apply to any stream.  This would allow you to checksum any stream of data rather than limiting yourself to just files.  That is how the crypto and compression streams work in .NET and provides more usability for them.

     

    Michael Taylor - 10/10/07

    http://p3net.mvps.org

     

    Wednesday, October 10, 2007 12:35 PM

All replies

  • There are many different ways to do what you want.  Your code streams the file one long int at a time.  For each value you update the checksum.  To do that in .NET you'd do the same thing.  Which class you actually use to stream/read the file doesn't really matter but I'm partial to BinaryReader in this case since you want to read bytes.  The Stream class is the underlying implementation but you gain nothing by using it directly.

     

    using(BinaryReader rdr = new BinaryReader(File.OpenReade(<path>)))
    {

       while (rdr.BaseStream.Position < rdr.BaseStream.Length)
       {

          ulong value = rdr.ReadUInt64();

          //Do work

       };
    };

     

    BinaryReader (nor the underlying Stream) doesn't expose an end of file property like StreamReader does.  Don't ask me why.  Therefore you'll need to compare the position to the length.

     

    Note that if the file isn't that big then you could also use File.ReadAllBytes to load the entire file into a byte array where you can process it faster.  This only works for small files though.

     

    Also be aware that most of the time you would create a stream decorator that you can then apply to any stream.  This would allow you to checksum any stream of data rather than limiting yourself to just files.  That is how the crypto and compression streams work in .NET and provides more usability for them.

     

    Michael Taylor - 10/10/07

    http://p3net.mvps.org

     

    Wednesday, October 10, 2007 12:35 PM
  •  TaylorMichaelL wrote:
    The Stream class is the underlying implementation but you gain nothing by using it directly.

     

     

    BinaryReader (nor the underlying Stream) doesn't expose an end of file property like StreamReader does. 

     

    I guess that makes a case for using both classes then?  What about using BinaryReader.PeekChar ?

     

    Note that if the file isn't that big then you could also use File.ReadAllBytes to load the entire file into a byte array where you can process it faster.  This only works for small files though.

     

    The files can be about 500K max.

     

    Thanks for the quick and detailed reposnse.

    Wednesday, October 10, 2007 12:57 PM
  • I would consider 500K to be to big to load into memory unless you need to load the data anyway.  If you go the stream decorator route then it isn't an option anyway.  The above code should work for your need.  You could use PeekChar if you'd like.  Just remember that -1 indicates no more data so don't confuse that with valid data.  Still I think you'll find a field comparison faster as a peek requires that the next character be read and then the stream pointer moved back.

     

    Michael Taylor - 10/10/07

    http://p3net.mvps.org

     

    Wednesday, October 10, 2007 1:17 PM