简体   繁体   English

遍历整个文件,同时为每个实例打印输出

[英]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. 输出现在转到txtbox。 Is that the best choice for a lengthy output? 这是长输出的最佳选择吗? I know, more than the original question. 我知道的,不仅仅是最初的问题。 But #1 is top priority. 但是#1是头等大事。 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 准备将其命名为“夜晚”,但会在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 . 如果找到第一个实例,则退出函数并丢弃BinaryReader If you call your function again, a new reader is created at position 0. 如果再次调用函数,则会在位置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 : 要显示所有找到的值,必须将它们合并为一个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()或使用LINQ的Select方法:

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. 对于第二个问题:只需将TextBox的转换后的text属性作为函数参数传递给findOffSet方法。

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). 并回答您的第三个问题:我认为TextBox可以处理少量结果,但是如果您的搜索结果有些复杂,则应该考虑使用ListBox如果有更多结果,甚至应该使用DataGridView (在这种情况下不适用)据我所知)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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