C# BinaryWriter Seek
Description
BinaryWriter Seek
Sets the position within the current
stream.
Syntax
BinaryWriter.Seek
has the following syntax.
public virtual long Seek(
int offset,
SeekOrigin origin
)
Parameters
BinaryWriter.Seek
has the following parameters.
offset
- A byte offset relative to origin.origin
- A field of SeekOrigin indicating the reference point from which the new position is to be obtained.
Returns
BinaryWriter.Seek
method returns The position with the current stream.
Example
The following example writes a series of byte values to a file.
// www . j av a 2s .c o m
using System;
using System.IO;
using System.Text;
public class BinReadWrite
{
public static void Main()
{
string testfile = @"C:\testfile.bin";
FileStream fs = File.Create(testfile);
UTF8Encoding utf8 = new UTF8Encoding();
BinaryWriter bw = new BinaryWriter(fs, utf8);
int pos;
for (pos = 0; pos < 128; pos++)
{
bw.Write((byte)pos);
}
for (pos = 0; pos < 120; pos += 8)
{
bw.Seek(7, SeekOrigin.Current);
bw.Write((byte)255);
}
fs.Seek(0, SeekOrigin.Begin);
byte[] rawbytes = new byte[fs.Length];
fs.Read(rawbytes, 0, (int)fs.Length);
int i = 0;
foreach (byte b in rawbytes)
{
Console.WriteLine("{0:d3} ", b);
}
fs.Close();
}
}