Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
BinaryReader.ReadByte()
Message
De
06/12/2004 15:44:24
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00967245
Message ID:
00967311
Vues:
7
>I am reading bytes from a data file using the ReadByte() method, but I am having some trouble finding out when the end of the data file has been reached. What I do now seems like a lot of work.
>
>
>string sFileName = "C:\\NMCC\\MWD\\data20041203134748.mwd";
>
>// Read the file using the stream reader to get the length of the file
>System.IO.StreamReader oReader = System.IO.File.OpenText(sFileName);
>string lcString = oReader.ReadToEnd();
>oReader.Close();
>
>// Use the length to the string to determine size of byte[]
>System.Byte[] myByte = new System.Byte[lcString.Length];
>System.IO.FileStream fs = new System.IO.FileStream(sFileName,System.IO.FileMode.Open, System.IO.FileAccess.Read);
>System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
>
>// ReadByte() in a for loop
>for (int i = 0 ; i < lcString.Length; i++)
>{
>	myByte[i] = r.ReadByte();
>}
>
>
>I know I can use the ReadBytes() method to get rid of the for loop but I would still need a way to find out how many bytes to read.
>
>What would be nice is if I can use a while loop around the ReadByte() method, but the ReadByte() method returns a System.Byte and I can't find anything to test for to check if I have reached the end.
>
>I can catch the an exception that the ReadByte() throws if the method is executed after EOF is reached.
>
>Any help would be appreciated.
>
>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