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.