Επεξεργασία

Κοινή χρήση μέσω


BinaryWriter.Seek(Int32, SeekOrigin) Method

Definition

Sets the position within the current stream.

public:
 virtual long Seek(int offset, System::IO::SeekOrigin origin);
public virtual long Seek (int offset, System.IO.SeekOrigin origin);
abstract member Seek : int * System.IO.SeekOrigin -> int64
override this.Seek : int * System.IO.SeekOrigin -> int64
Public Overridable Function Seek (offset As Integer, origin As SeekOrigin) As Long

Parameters

offset
Int32

A byte offset relative to origin.

origin
SeekOrigin

A field of SeekOrigin indicating the reference point from which the new position is to be obtained.

Returns

The position with the current stream.

Exceptions

The file pointer was moved to an invalid location.

The SeekOrigin value is invalid.

Examples

The following example writes a series of byte values to a file. The example uses Seek to move to various locations in the file, and then writes marker bytes by using the Write method.

using namespace System;
using namespace System::IO;
using namespace System::Text;

public ref class BinReadWrite
{
public:
    static void Main()
    {
        String^ testfile = "C:\\temp\\testfile.bin";

        // create a test file using BinaryWriter
        FileStream^ fs = File::Create(testfile);
        UTF8Encoding^ utf8 = gcnew UTF8Encoding();

        BinaryWriter^ bw = gcnew BinaryWriter(fs, utf8);
        // write a series of bytes to the file, each time incrementing
        // the value from 0 - 127
        int pos;

        for (pos = 0; pos < 128; pos++)
        {
            bw->Write((Byte)pos);
        }

        // reset the stream position for the next write pass
        bw->Seek(0, SeekOrigin::Begin);
        // write marks in file with the value of 255 going forward
        for (pos = 0; pos < 120; pos += 8)
        {
            bw->Seek(7, SeekOrigin::Current);
            bw->Write((Byte)255);
        }

        // reset the stream position for the next write pass
        bw->Seek(0, SeekOrigin::End);
        // write marks in file with the value of 254 going backward
        for (pos = 128; pos > 6; pos -= 6)
        {
            bw->Seek(-6, SeekOrigin::Current);
            bw->Write((Byte)254);
            bw->Seek(-1, SeekOrigin::Current);
        }

        // now dump the contents of the file using the original file stream
        fs->Seek(0, SeekOrigin::Begin);
        array<Byte>^ rawbytes = gcnew array<Byte>(fs->Length);
        fs->Read(rawbytes, 0, (int)fs->Length);

        int i = 0;
        for each (Byte b in rawbytes)
        {
             switch (b)
             {
                 case 254:
                 {
                     Console::Write("-%- ");
                 }
                 break;

                 case 255:
                 {
                     Console::Write("-*- ");
                 }
                 break;

                 default:
                 {
                     Console::Write("{0:d3} ", b);
                 }
                 break;
             }
             i++;
             if (i == 16)
             {
                 Console::WriteLine();
                 i = 0;
             }
        }
        fs->Close();
    }
};

int main()
{
    BinReadWrite::Main();
}

//The output from the program is this:
//
// 000 001 -%- 003 004 005 006 -*- -%- 009 010 011 012 013 -%- -*-
// 016 017 018 019 -%- 021 022 -*- 024 025 -%- 027 028 029 030 -*-
// -%- 033 034 035 036 037 -%- -*- 040 041 042 043 -%- 045 046 -*-
// 048 049 -%- 051 052 053 054 -*- -%- 057 058 059 060 061 -%- -*-
// 064 065 066 067 -%- 069 070 -*- 072 073 -%- 075 076 077 078 -*-
// -%- 081 082 083 084 085 -%- -*- 088 089 090 091 -%- 093 094 -*-
// 096 097 -%- 099 100 101 102 -*- -%- 105 106 107 108 109 -%- -*-
// 112 113 114 115 -%- 117 118 -*- 120 121 -%- 123 124 125 126 127
using System;
using System.IO;
using System.Text;

public class BinReadWrite3
{
    public static void Main()
    {
        string testfile = @"C:\temp\testfile.bin";

        // create a test file using BinaryWriter
        FileStream fs = File.Create(testfile);
        UTF8Encoding utf8 = new UTF8Encoding();

        BinaryWriter bw = new BinaryWriter(fs, utf8);
        // write a series of bytes to the file, each time incrementing
        // the value from 0 - 127
        int pos;

        for (pos = 0; pos < 128; pos++)
        {
            bw.Write((byte)pos);
        }

        // reset the stream position for the next write pass
        bw.Seek(0, SeekOrigin.Begin);
        // write marks in file with the value of 255 going forward
        for (pos = 0; pos < 120; pos += 8)
        {
            bw.Seek(7, SeekOrigin.Current);
            bw.Write((byte)255);
        }

        // reset the stream position for the next write pass
        bw.Seek(0, SeekOrigin.End);
        // write marks in file with the value of 254 going backward
        for (pos = 128; pos > 6; pos -= 6)
        {
            bw.Seek(-6, SeekOrigin.Current);
            bw.Write((byte)254);
            bw.Seek(-1, SeekOrigin.Current);
        }

        // now dump the contents of the file using the original file stream
        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)
        {
             switch (b)
             {
                 case 254:
                 {
                     Console.Write("-%- ");
                 }
                 break;

                 case 255:
                 {
                     Console.Write("-*- ");
                 }
                 break;

                 default:
                 {
                     Console.Write("{0:d3} ", b);
                 }
                 break;
             }
             i++;
             if (i == 16)
             {
                 Console.WriteLine();
                 i = 0;
             }
        }
        fs.Close();
    }
}

//The output from the program is this:
//
// 000 001 -%- 003 004 005 006 -*- -%- 009 010 011 012 013 -%- -*-
// 016 017 018 019 -%- 021 022 -*- 024 025 -%- 027 028 029 030 -*-
// -%- 033 034 035 036 037 -%- -*- 040 041 042 043 -%- 045 046 -*-
// 048 049 -%- 051 052 053 054 -*- -%- 057 058 059 060 061 -%- -*-
// 064 065 066 067 -%- 069 070 -*- 072 073 -%- 075 076 077 078 -*-
// -%- 081 082 083 084 085 -%- -*- 088 089 090 091 -%- 093 094 -*-
// 096 097 -%- 099 100 101 102 -*- -%- 105 106 107 108 109 -%- -*-
// 112 113 114 115 -%- 117 118 -*- 120 121 -%- 123 124 125 126 127
open System.IO
open System.Text

let testfile = @"C:\temp\testfile.bin"

// create a test file using BinaryWriter
let fs = File.Create testfile
let utf8 = UTF8Encoding()

let bw = new BinaryWriter(fs, utf8)
// write a series of bytes to the file, each time incrementing
// the value from 0 - 127

for pos = 0 to 127 do
    bw.Write(byte pos)

// reset the stream position for the next write pass
bw.Seek(0, SeekOrigin.Begin) |> ignore
// write marks in file with the value of 255 going forward
for _ in 0..8..119 do
    bw.Seek(7, SeekOrigin.Current) |> ignore
    bw.Write(byte 255)

// reset the stream position for the next write pass
bw.Seek(0, SeekOrigin.End) |> ignore
// write marks in file with the value of 254 going backward
for _ in 128 .. -6 .. 6 do
    bw.Seek(-6, SeekOrigin.Current) |> ignore
    bw.Write(byte 254)
    bw.Seek(-1, SeekOrigin.Current) |> ignore

// now dump the contents of the file using the original file stream
fs.Seek(0, SeekOrigin.Begin) |> ignore
let rawbytes = Array.zeroCreate<byte> (int fs.Length)
fs.Read(rawbytes, 0, int fs.Length) |> ignore

let mutable i = 0
for b in rawbytes do
    match b with
    | 254uy ->
        printf "-%%- "
    | 255uy ->
        printf "-*- "
    | _ ->
        printf $"{b:d3} "
    i <- i + 1
    if i = 16 then
        printfn ""
        i <- 0
fs.Close()

//The output from the program is this:
//
// 000 001 -%- 003 004 005 006 -*- -%- 009 010 011 012 013 -%- -*-
// 016 017 018 019 -%- 021 022 -*- 024 025 -%- 027 028 029 030 -*-
// -%- 033 034 035 036 037 -%- -*- 040 041 042 043 -%- 045 046 -*-
// 048 049 -%- 051 052 053 054 -*- -%- 057 058 059 060 061 -%- -*-
// 064 065 066 067 -%- 069 070 -*- 072 073 -%- 075 076 077 078 -*-
// -%- 081 082 083 084 085 -%- -*- 088 089 090 091 -%- 093 094 -*-
// 096 097 -%- 099 100 101 102 -*- -%- 105 106 107 108 109 -%- -*-
// 112 113 114 115 -%- 117 118 -*- 120 121 -%- 123 124 125 126 127
Imports System.IO
Imports System.Text

Public Class BinReadWrite
    Public Shared Sub Main()
        Dim testfile As String = "C:\temp\testfile.bin"

        ' create a test file using BinaryWriter
        Dim fs As FileStream = File.Create(testfile)
        Dim utf8 As New UTF8Encoding()

        Dim bw As New BinaryWriter(fs, utf8)
        ' write a series of bytes to the file, each time incrementing
        ' the value from 0 - 127
        Dim pos As Integer

        For pos = 0 to 127
            bw.Write(CType(pos, Byte))
        Next pos

        ' reset the stream position for the next write pass
        bw.Seek(0, SeekOrigin.Begin)
        ' write marks in file with the value of 255 going forward
        For pos = 0 To 119 Step 8
            bw.Seek(7, SeekOrigin.Current)
            bw.Write(CType(255, Byte))
        Next pos

        ' reset the stream position for the next write pass
        bw.Seek(0, SeekOrigin.End)
        ' write marks in file with the value of 254 going backward
        For pos = 128 To 7 Step -6
            bw.Seek(-6, SeekOrigin.Current)
            bw.Write(CType(254, Byte))
            bw.Seek(-1, SeekOrigin.Current)
        Next pos

        ' now dump the contents of the file using the original file stream
        fs.Seek(0, SeekOrigin.Begin)
        Console.WriteLine("Length: {0:d}", fs.Length)
        Dim rawbytes(fs.Length) As Byte
        fs.Read(rawbytes, 0, fs.Length)
        Console.WriteLine("Length: {0:d}", rawbytes.Length)

        Dim i As Integer = 0
        For Each b As Byte In rawbytes
             Select b
                 Case 254
                     Console.Write("-%- ")

                 Case 255
                     Console.Write("-*- ")

                 Case Else
                     Console.Write("{0:d3} ", b)
             End Select
             i = i + 1
             If i = 16 Then
                 Console.WriteLine()
                 i = 0
             End If
        Next b
        fs.Close()
    End Sub
End Class

' The output from the program is this:
'
' 000 001 -%- 003 004 005 006 -*- -%- 009 010 011 012 013 -%- -*-
' 016 017 018 019 -%- 021 022 -*- 024 025 -%- 027 028 029 030 -*-
' -%- 033 034 035 036 037 -%- -*- 040 041 042 043 -%- 045 046 -*-
' 048 049 -%- 051 052 053 054 -*- -%- 057 058 059 060 061 -%- -*-
' 064 065 066 067 -%- 069 070 -*- 072 073 -%- 075 076 077 078 -*-
' -%- 081 082 083 084 085 -%- -*- 088 089 090 091 -%- 093 094 -*-
' 096 097 -%- 099 100 101 102 -*- -%- 105 106 107 108 109 -%- -*-
' 112 113 114 115 -%- 117 118 -*- 120 121 -%- 123 124 125 126 127

Remarks

For a list of common I/O tasks, see Common I/O Tasks.

Applies to

See also