-2

我有一个 8mb 的文件,其中包含一些记录。我想从特定位置读取特定记录。我有文件中每条记录的开始和结束字节索引。

我的问题是如何使用文件对话框来选择特定文件并创建一个读取文件然后将特定记录存储在文本框中的函数。

我对如何一次读取所有文本框中的所有记录也有疑问。

4

1 回答 1

0

首先,我需要一个我的文件值的索引,假设产品 id =id09876543 location =india然后id09876543 的索引值为“12 到 23” ,然后我将在函数调用中传递 12 和 23。

  1. 使一个名为“read_value”的用户定义函数以整数形式传递其中的 2 参数并将函数作为字符串,即它将以字符串格式返回值。

  2. 在您想要答案的特定位置调用该函数。

像这样。

1)

公共函数 read_value(ByVal strat As Integer, ByVal end1 As Integer) As String

    Dim fs As FileStream = New FileStream(f_name, FileMode.Open, FileAccess.Read)

    Dim n As Integer = 0
    Dim s As String = Nothing
    Dim i As Integer = 0
    Dim l As Long = strat
    fs.Seek(l, SeekOrigin.Begin)
    'Seek(strat)
    For i = strat To end1
        n = fs.ReadByte()
        s = s + Convert.ToChar(n)
    Next
    Return s
End Function

Dim ofd1 As New OpenFileDialog ' Dim file_name As String Try If ofd1.ShowDialog = Windows.Forms.DialogResult.OK Then f_name = ofd1.FileName

            product_id_txt.Text = read_value(12, 23)
            location_txt.Text = read_value(34, 50)
            form.Show()
        End If
    Catch ex As Exception
        MessageBox.Show("File Not Found")
    End Try

此代码的输出是 label----->Product Id: id09876543 <---- 这是我的文本框值

于 2013-03-09T08:21:47.870 回答