Illustrates random access to a file : Random Read « File Stream « C# / C Sharp






Illustrates random access to a file

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();
  }
}

           
       








Related examples in the same category