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
>>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.