Read every other value using FileSeek in CSharp

Description

The following code shows how to read every other value using FileSeek.

Example


 //from  w  ww .  j a v a  2s.  c  o  m
using System; 
using System.IO;  
 
class MainClass { 
  public static void Main() { 
    FileStream f = new FileStream("random.dat", FileMode.Create); 
    char ch; 
    for(int i=0; i < 26; i++) { 
        f.WriteByte((byte)('A'+i)); 
    } 
    Console.WriteLine("Here is every other value: "); 
    for(int i=0; i < 26; i += 2) { 
        f.Seek(i, SeekOrigin.Begin); // seek to ith double 
        ch = (char) f.ReadByte(); 
        Console.Write(ch + " "); 
    } 
    f.Close(); 
  } 
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip