Im stumped with some array issue thingy....


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Im stumped with some array issue thingy....

  1. #1
    Jay King Guest

    Im stumped with some array issue thingy....

    Ok what I'm doing is loading the first 32 bytes of a file looking for a
    string then I start over again and from the last position (minus a few
    bytes ) I start searching again. My problem is that it errors the second
    time around claiming it (the file io object) cant load the next 32 bytes
    into the array as if its already filled or some idiotic thing... HELP!

    Here is my code. Use any file for testing longer then 64 bytes

    Imports System.IO

    Imports System.Text.ASCIIEncoding

    Module Module1

    Const strtBlock = "MADk" & Chr(80)

    Const endBlock = "SCDlt"

    Sub Main()

    Console.WriteLine("Simcity 4 .dat file parter, Copyright 2003 CPG")

    Console.WriteLine("Enter the name of the .dat file you wish to split")

    Dim filename As String

    filename = Console.ReadLine()

    Console.WriteLine("About to process file...")

    Dim fs As New FileStream(filename, FileMode.Open)

    Dim datfile As IO.BinaryReader

    Dim tempByte() As Byte '= New Byte(32) {}

    Dim tempString As String

    Dim index As Integer = 1

    Dim count As Integer = 32

    Dim stringExists As Integer ' non zero if true

    10: Threading.Thread.CurrentThread.Sleep(New TimeSpan(10)) 'Sleep for 10
    ticks (100 nano seconds * 10, 1,000 nanoseconds)

    'tempByte = New Byte(32) {} 'Create new array of 32 bytes

    tempByte = getbytearray()

    datfile = New IO.BinaryReader(fs)

    If (datfile.BaseStream.Length - index) < 32 Then count =
    (datfile.BaseStream.Length - index) - 32 'Making sure count is never longer
    then length of file.

    datfile.Read(tempByte, index, count)

    tempString = ASCII.GetChars(tempByte) 'Turn my byte array into a string

    Erase tempByte

    Console.WriteLine(tempString)

    stringExists = InStr(tempString, strtBlock, CompareMethod.Binary)

    If stringExists = 0 Then

    If count < 32 Then

    Console.WriteLine("End Searching file")

    Console.Read()

    End

    End If

    index = index + (32 - 5)

    GoTo 10

    Else 'I have found the start Block

    Console.WriteLine("Found Start Block @" & index + stringExists) 'Tell user
    you have found start byte.

    End If

    End Sub

    Function getbytearray() As Byte()

    Dim tempByte() As Byte = New Byte(32) {}

    Return tempByte

    End Function

    End Module



  2. #2
    iGadget Guest

    Re: Im stumped with some array issue thingy....

    On Sat, 18 Jan 2003 05:18:21 -0500, Jay King wrote:

    > Ok what I'm doing is loading the first 32 bytes of a file looking for a
    > string then I start over again and from the last position (minus a few
    > bytes ) I start searching again. My problem is that it errors the second
    > time around claiming it (the file io object) cant load the next 32 bytes
    > into the array as if its already filled or some idiotic thing... HELP!


    In the line
    datfile.Read(tempByte, index, count)
    the index variable contains the position in the array that you want to
    read into, not the position in the input file.

    You need to use something like

    Dim datfile As IO.BufferedStream
    datfile = New BufferedStream(fs)
    ..
    datfile.Seek(index, SeekOrigin.Begin)
    datfile.Read(tempByte, 0, count)

    Cheers,
    Jason

  3. #3
    Jay King Guest

    Re: Im stumped with some array issue thingy....

    Sweet Thanks!

    "iGadget" <igadget_@hotmail.com> wrote in message
    news:w4n9e6r8ndiy$.bda43ym6lzdy$.dlg@40tude.net...
    | On Sat, 18 Jan 2003 05:18:21 -0500, Jay King wrote:
    |
    | > Ok what I'm doing is loading the first 32 bytes of a file looking for a
    | > string then I start over again and from the last position (minus a few
    | > bytes ) I start searching again. My problem is that it errors the
    second
    | > time around claiming it (the file io object) cant load the next 32 bytes
    | > into the array as if its already filled or some idiotic thing... HELP!
    |
    | In the line
    | datfile.Read(tempByte, index, count)
    | the index variable contains the position in the array that you want to
    | read into, not the position in the input file.
    |
    | You need to use something like
    |
    | Dim datfile As IO.BufferedStream
    | datfile = New BufferedStream(fs)
    | .
    | datfile.Seek(index, SeekOrigin.Begin)
    | datfile.Read(tempByte, 0, count)
    |
    | Cheers,
    | Jason



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center