简体   繁体   中英

Loop through entire file while printing output for each instance

  1. The title pretty much covers it, I am searching an array and want to print the output for every instance found. My current code finds the specified value and returns its location, however stops after the first instance is found and I want them all.

  2. Also is there a way to use a textbox for my search value? Or if I use a textbox will it always search as a string? Bc that will not do :(

  3. Output goes to a txtbox right now. Is that the best choice for a lengthy output? I know, more than the original question. But #1 is top priority. Any help is welcome and appreciated.

My Current Code =

 Private Function findOffset()
    Using reader As New BinaryReader(File.Open("FilePath", FileMode.Open, FileAccess.Read))
        Dim pos As Integer = 0
        Dim length As Integer = reader.BaseStream.Length
        Do While pos < length
            Dim value As Byte = reader.ReadByte()
            If value = CByte(&H13) Then
                Return pos
                Exit Do
            End If
            pos += 1
        Loop

    End Using
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = (Hex(findOffset()).ToString.PadLeft(6, "0"c))

End Sub

Getting ready to call it a night but will check back first thing in the AM

Your first problem lies here:

If value = CByte(&H13) Then
  Return pos
  Exit Do
End If

If you find the first instance you exit your function and throw away your BinaryReader . If you call your function again, a new reader is created at position 0.

Try changing your code to this:

Dim positions As New List(Of Integer)()

[...]

If value = CByte(&H13) Then
  positions.Add(pos)
End If

This way you get all occurences.

To show all the values you found you have to concat them into one String :

TextBox1.Text = String.Join("|", findOffset())

If you want to convert your integer values in a certain way you could either loop over findOffset() or use LINQ's Select method:

findOffset().Select(Function(i) Hex(i).ToString.PadLeft(6, "0"c))

For your second problem: just pass a TextBox 's convertd text property as a function parameter to your findOffSet method.

Private Function findOffset(ByVal search as Byte)
  [...]
  If value = search Then
  [...]
End Function

And call it like this:

findOffset(Convert.ToByte(Convert.ToInt16(mySearchTextBox.Text)))

You have to check for valid text inputs beforehand or you will get an exception at this line.

And to answer your third question: I think a TextBox is ok for a small amount of results but you should consider using a ListBox if you have a few more or even a DataGridView if your search results are somewhat complex (not in this particular case as far as I can see).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM