Illustrates random access to a file

image_pdfimage_print
   

using System;
using System.IO;

public class RandomAccess {
  public static void Main(String[] args) {
    FileStream fileStream = new FileStream("test.cs", FileMode.Open);
    fileStream.Seek(20, SeekOrigin.Begin);
    
    byte[] anInt = new byte[4];
    fileStream.Read(anInt,0,4);
    int number = anInt[3];
    
    for(int i = 2; i >= 0; i--){
      number *= 256;
      number += anInt[i];
    }     
    Console.WriteLine("The number starting at byte 20 is {0}",  number);
    fileStream.Close();
  }
}

           
          


This entry was posted in File Stream. Bookmark the permalink.