Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
BinaryReader.ReadByte()
Message
De
07/12/2004 07:11:08
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00967245
Message ID:
00967419
Vues:
7
This message has been marked as the solution to the initial question of the thread.
Einar,
No particular reason. In notes I tried to mean that:) There are tons of classes, methods doing the same thing at the end. If I knew what you were doing after reading might come out with something totally different:)
I didn't used ReadByte for one particular reason anyway, it reads one byte. Thought it'd not be efficient. ReadBytes OTOH might be more suitable for performance.
In buffered read you can use any block size (4096 is also the smallest storage size on disk on most systems, and exact multiple of K etc). Whatever your actual data length is it works.
Say you set buffer length as 1192 and actual data is 1193 bytes. It'd read the first 1192 bytes into buffer array. Then on next 'line' it'd only read 1 byte and rest of the buffer array elements would be the values left over previous read. Since using the return value intBytesRead you know how many array elements to check as part of actual data.
Below is ReadByte,ReadBytes ways:
using System;
using System.IO;

class test
{
 public static void Main()
 {
  string sFileName = "C:\\NMCC\\MWD\\data20041203134748.mwd";
  test tester = new test();

  Console.WriteLine("Reading in chunks");
  tester.readBytes(sFileName);

  Console.WriteLine("Reading byte by byte");
  tester.readByte_By_Byte(sFileName);
 }

 public void readBytes(string sFileName)
 {
  const int buflen=4096;
  FileStream fs = new FileStream(sFileName, FileMode.Open, FileAccess.Read);
  BinaryReader br = new BinaryReader(fs);
  
  while ( fs.Position < fs.Length ) // while not EOF
  {
     byte[] myBytes = br.ReadBytes(buflen); // request N bytes
     Console.Write("Read {0} bytes:",myBytes.Length); // myBytes array length is the 
                                                      // actual number read
     for(int i=0;i<myBytes.Length;i++)
     {
        Console.Write( "[{0,3}]",myBytes[i]);
     }
     Console.WriteLine();
  }
  fs.Close();
 }

 public void readByte_By_Byte(string sFileName)
 {
  FileStream fs = new FileStream(sFileName, FileMode.Open, FileAccess.Read);
  BinaryReader br = new BinaryReader(fs);
  
  while ( fs.Position < fs.Length )
  {
     Console.Write("[{0,3}]",br.ReadByte());
  }
  fs.Close();
 }
}
Cetin

>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
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform