Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
BinaryReader.ReadByte()
Message
 
To
06/12/2004 15:44:24
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00967245
Message ID:
00967346
Views:
7
Cetin,
Thanks for your reply. Out of curiosity why would you not use the ReadByte() method?
I need to read data in 1192-bytes blocks and the problem is that there could be a variable number of blocks in the file (which your method seems to solve). I will test this further tomorrow.

Are there any advantages/disadvantages using the BinaryReader.ReadByte() vs. using the FileStream.Read() method?

Thanks,
Einar

>
>Einar,
>Assuming you want it as a byte[] and do a buffered read:
>
>using System;
>using System.IO;
>class test
>{
>  static void Main()
>  {
>    const int buflen=4096; // read in 4K blocks
>    byte[] myBytes = new byte[buflen]; // buffer to read into
>    int bytesRead; // will hold actual number of bytes read
>    string sFileName = "C:\\NMCC\\MWD\\data20041203134748.mwd";
>
>    FileStream fs = File.OpenRead(sFileName); // Open file for reading
>    while( (bytesRead = fs.Read( myBytes,0,buflen )) > 0 ) // while there are bytes read
>    {
>       for(int i=0;i < bytesRead;i++)
>        {Console.Write( "[{0,3}]",myBytes[i].ToString() );}
>    }
>  }
>}
>
bytesRead is the number of bytes read from stream. If 0, eof. It would be less than 'buflen' if fewer bytes left on stream.
>
>PS: There are numerous ways to do this in .NET. This is just one way. If say you wanted it as a char array and as in your sample used ReadToEnd() to get a string, you could do this too:
>
>char[] myChars = lcString.ToCharArray();
>
>or:
>
>char[] myChars = oReader.ReadToEnd().ToCharArray();
>
>However buffered read sounds more appropriate in many cases.
>
>Cetin
Semper ubi sub ubi.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform