Byte-Oriented File input and output : Byte Read Write « File Stream « C# / C Sharp






Byte-Oriented File input and output

Byte-Oriented File input and output
 

using System;
using System.IO;

class ShowFile {
  public static void Main(string[] args) {
    int i;
    FileStream fin;

    try {
      fin = new FileStream("test.cs", FileMode.Open);
    } catch(FileNotFoundException exc) {
      Console.WriteLine(exc.Message);
      return;
    } catch(IndexOutOfRangeException exc) {
      Console.WriteLine(exc.Message + "\nUsage: ShowFile File");
      return;
    }

    // read bytes until EOF is encountered
    do {
      try {
        i = fin.ReadByte();
      } catch(Exception exc) {
        Console.WriteLine(exc.Message);
        return;
      }
      if(i != -1) Console.Write((char) i);
    } while(i != -1);

    fin.Close();
  }
}
           
         
  








Related examples in the same category

1.Byte-Oriented: Write to a file
2.Save byte array to a file
3.Write byte array to a file
4.Compare two files byte by byte
5.Read file content to a byte array
6.Read binary context of a file.
7.Save a byte array content into a file.