Home   Cover Cover Cover Cover
 

Reading a file

Chap4_Ex6.cs
using System;
using System.IO;
using System.Text;

public class More {

  public static void Main(string[] args) {
    if(args.Length==0) Console.WriteLine("file?");
    else {
      if(!File.Exists(args[0])) Console.WriteLine("file does not exist!");
      else {
        FileStream fs = File.OpenRead(args[0]);
        byte[] data = new byte[fs.Length];
        int pos = 0;
        int b = fs.ReadByte();
        while(b>0) {
          data[pos] = (byte)b;
          pos++;
          b = fs.ReadByte();
        }
        fs.Close();
        System.Console.Write(ASCIIEncoding.ASCII.GetString(data));
      }
    }
  }
}