Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VB.NET中正确读取随机访问文件_Vb.net - Fatal编程技术网

如何在VB.NET中正确读取随机访问文件

如何在VB.NET中正确读取随机访问文件,vb.net,Vb.net,我试图读取一个随机访问文件,但在第一个文件上出现以下错误错误5(无法读取流末尾以外的内容)。我不确定我在这里做错了什么,我该如何解决这个问题 Structure StdSections 'UPGRADE_WARNING: Fixed-length string size must fit in the buffer. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="3C1E442

我试图读取一个随机访问文件,但在第一个文件上出现以下错误
错误5(无法读取流末尾以外的内容)
。我不确定我在这里做错了什么,我该如何解决这个问题

Structure StdSections
    'UPGRADE_WARNING: Fixed-length string size must fit in the buffer. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="3C1E4426-0B80-443E-B943-0627CD55D48B"'
    <VBFixedString(15), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=15)> Public A() As Char 'BEAM  --- complete beam designation          15
    'UPGRADE_WARNING: Fixed-length string size must fit in the buffer. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="3C1E4426-0B80-443E-B943-0627CD55D48B"'
    <VBFixedString(2), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=2)> Public B() As Char 'DSG   --- shape  ie "W" or "C"                2
    Dim C As Single 'DN    --- nominal depth of section            4
    Dim d As Single 'WGT   --- weight                              4
    .
    .
    .
End structure
''Note 'File1'is the existing RAF and holds complete path!

        Dim i,ffr,fLength,lastmembNo as integer
        sectionFound = False
        Dim std As new StdSections 
        fLength = Len(std)
        If fLength = 0 Then fLength = 168 ' 177
        ffr = FreeFile()
        FileOpen(ffr, File1, OpenMode.Random, OpenAccess.Read, OpenShare.LockRead, fLength)
        lastmembNo = CInt(LOF(ffr)) \ fLength

        For i = 1 To lastmembNo
            FileGet(ffr, std, i)
            >>Error 5 (unable to read beyond end of the stream) <<<                  
            If Trim(memberID) = Trim(std.A) Then
                    sectionFound = True
                end if
        next i
结构标准部分
'升级\u警告:固定长度字符串大小必须适合缓冲区。点击查看更多信息:“ms”-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword=“3C1E4426-0B80-443E-B943-0627CD55D48B”
公共A()作为字符光束——完整光束名称15
'升级\u警告:固定长度字符串大小必须适合缓冲区。点击查看更多信息:“ms”-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword=“3C1E4426-0B80-443E-B943-0627CD55D48B”
公共B()作为字符DSG---形状即“W”或“C”2
尺寸C为单个“DN”——截面4的标称深度
尺寸d为单件WGT--重量4
.
.
.
端部结构
''注意'File1'是现有RAF,拥有完整路径!
Dim i、ffr、FLENGHT、lastmembNo作为整数
sectionFound=False
将标准尺寸标注为新标准段
长度=长度(标准)
如果FLENGHT=0,则FLENGHT=168'177
ffr=FreeFile()
FileOpen(ffr、File1、OpenMode.Random、OpenAccess.Read、OpenShare.LockRead、fLength)
lastmembNo=CInt(LOF(ffr))\f长度
对于i=1至lastmembNo
文件获取(ffr、std、i)

>>错误5(无法读取流末尾以外的内容)Wow Freefile!那是过去的爆炸

我没有在VB.NET中真正使用旧的OpenFile等文件访问方法,所以我只是猜测,但在.NET中,许多变量类型的大小都发生了变化。e、 一个整数现在是32位(4字节),我认为布尔值是不同的,尽管一个仍然是4字节

此外,.NET中的字符串默认为Unicode格式,而不是ASCII格式,因此不能依赖.NET字符串变量中的1个字符=1个字节。事实上,.NET实际上是在运行前在PC上“JIT编译”程序,所以你不能像以前那样在内存中轻松地布局结构

如果您想切换到新的基于“流”的对象,以下是一些代码:

    Dim strFilename As String = "C:\Junk\Junk.txt"
    Dim strTest As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Call My.Computer.FileSystem.WriteAllText(strFilename, strTest, False)
    Dim byt(2) As Byte
    Using fs As New FileStream(strFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
      fs.Seek(16, SeekOrigin.Begin)
      fs.Read(byt, 0, 3)
      Dim s As String = Chr(byt(0)) & Chr(byt(1)) & Chr(byt(2))
      MsgBox(s)
      fs.Seek(5, SeekOrigin.Begin)
      fs.Write(byt, 0, 3)
    End Using
    Dim strModded As String = My.Computer.FileSystem.ReadAllText(strFilename)
    MsgBox(strModded)

不过,我不会责怪您保留了旧方法:使用新方法,您需要定义一个类,然后有一个自定义例程将Byte()转换为类的属性。不仅仅是将字节从文件加载到内存中,还要做更多的工作。

哇,Freefile!那是过去的爆炸

我没有在VB.NET中真正使用旧的OpenFile等文件访问方法,所以我只是猜测,但在.NET中,许多变量类型的大小都发生了变化。e、 一个整数现在是32位(4字节),我认为布尔值是不同的,尽管一个仍然是4字节

此外,.NET中的字符串默认为Unicode格式,而不是ASCII格式,因此不能依赖.NET字符串变量中的1个字符=1个字节。事实上,.NET实际上是在运行前在PC上“JIT编译”程序,所以你不能像以前那样在内存中轻松地布局结构

如果您想切换到新的基于“流”的对象,以下是一些代码:

    Dim strFilename As String = "C:\Junk\Junk.txt"
    Dim strTest As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Call My.Computer.FileSystem.WriteAllText(strFilename, strTest, False)
    Dim byt(2) As Byte
    Using fs As New FileStream(strFilename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
      fs.Seek(16, SeekOrigin.Begin)
      fs.Read(byt, 0, 3)
      Dim s As String = Chr(byt(0)) & Chr(byt(1)) & Chr(byt(2))
      MsgBox(s)
      fs.Seek(5, SeekOrigin.Begin)
      fs.Write(byt, 0, 3)
    End Using
    Dim strModded As String = My.Computer.FileSystem.ReadAllText(strFilename)
    MsgBox(strModded)

不过,我不会责怪您保留了旧方法:使用新方法,您需要定义一个类,然后有一个自定义例程将Byte()转换为类的属性。不仅仅是将字节从文件加载到内存中,还要做更多的工作。

好的,我认为您应该切换到“.NET方式”,如下所示:

Imports System.IO
Imports System.Xml

Public Class Form1

  Public Const gintRecLen_CONST As Integer = 177

  Class StdSections2
    Private mstrA As String
    Public Property A() As String
      Get
        Return mstrA
      End Get
      Set(ByVal value As String)
        If value.Length <> 15 Then
          Throw New Exception("Wrong size")
        End If
        mstrA = value
      End Set
    End Property

    Private mstrB As String
    Public Property B() As String
      Get
        Return mstrB
      End Get
      Set(ByVal value As String)
        If value.Length <> 2 Then
          Throw New Exception("Wrong size")
        End If
        mstrB = value
      End Set
    End Property

    Public C(39) As Single

    Public Shared Function FromBytes(byt() As Byte) As StdSections2
      Dim output As New StdSections2

      If byt.Length <> gintRecLen_CONST Then
        Throw New Exception("Wrong size")
      End If
      For i As Integer = 0 To 14
        output.mstrA &= Chr(byt(i))
      Next i
      For i As Integer = 15 To 16
        output.mstrB &= Chr(byt(i))
      Next i
      For i As Integer = 0 To 39
        Dim bytTemp(3) As Byte
        output.C(i) = BitConverter.ToSingle(byt, 17 + 4 * i)
      Next i
      Return output
    End Function
  End Class

  Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.


  End Sub


  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim strFilename As String = "C:\Junk\Junk.txt"
    Dim strMemberID As String = "foo"
    Dim intRecCount As Integer = CInt(My.Computer.FileSystem.GetFileInfo(strFilename).Length) \ gintRecLen_CONST
    Dim blnSectionFound As Boolean = False
    Using fs As New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
      For intRec As Integer = 0 To intRecCount - 1
        Dim intRecPos As Integer = gintRecLen_CONST * intRec
        fs.Seek(intRecPos, SeekOrigin.Begin)
        Dim byt(gintRecLen_CONST - 1) As Byte
        fs.Read(byt, 0, gintRecLen_CONST)
        Dim ss2 As StdSections2 = StdSections2.FromBytes(byt)
        'MsgBox(ss2.A & ":" & ss2.C(3)) 'debugging
        If strMemberID.Trim = ss2.A.Trim Then
          blnSectionFound = True
          Exit For
        End If
      Next intRec
    End Using
    MsgBox(blnSectionFound.ToString)

  End Sub
End Class
Imports System.IO
导入System.Xml
公开课表格1
公共常数gintRecLen_Const As Integer=177
类StdSections2
私有mstrA作为字符串
作为字符串的公共属性A()
得到
返回mstrA
结束
设置(ByVal值作为字符串)
如果值为。长度为15,则
抛出新异常(“错误大小”)
如果结束
mstrA=值
端集
端属性
私有mstrB作为字符串
作为字符串的公共属性B()
得到
返回mstrB
结束
设置(ByVal值作为字符串)
如果值为。长度为2,则
抛出新异常(“错误大小”)
如果结束
mstrB=值
端集
端属性
公共C(39)作为单一
公共共享函数FromBytes(byt()作为Byte)作为StdSections2
作为新StdSections2的Dim输出
如果比亚迪长度是银树常数那么
抛出新异常(“错误大小”)
如果结束
对于i,整数=0到14
output.mstrA&=Chr(byt(i))
接下来我
对于i,整数=15到16
output.mstrB&=Chr(byt(i))
接下来我
对于i,整数=0到39
Dim bytTemp(3)作为字节
输出.C(i)=位转换器.ToSingle(byt,17+4*i)
接下来我
返回输出
端函数
末级
次新
'设计器需要此调用。
初始化组件()
'在InitializeComponent()调用之后添加任何初始化。
端接头
私有子按钮1\u单击(发送者作为System.Object,e作为System.EventArgs)处理按钮1。单击
Dim strFilename为String=“C:\Junk\Junk.txt”
将strMemberID设置为字符串=“foo”
Dim intRecCount As Integer=CInt(My.Computer.FileSystem.GetFileInfo(strFilename.Length)\gintRecLen\u CONST
Dim BLNSECTION发现为布尔值=False
将fs用作新文件流(strFilename、FileMode.Open、FileAccess.Read、FileShare.ReadWrite)
对于intRec As Integer=0到intRecCount-1
Dim intRecPos为整数=gintRecLen\u CONST*intRec
fs.Seek(intRecPos,SeekOrigin.Begin)
作为字节的Dim byt(gintRecLen_CONST-1)
财政司司长(比亚迪,0,金特雷克伦常数)
作为StdSections2的Dim ss2=StdSections2.FromBytes(byt)
'MsgBox(ss2.A&“:”&ss2.C(3))'调试
如果strMemberID.Trim=ss2.A.Trim,则
blnSectionFound=True
退出
如果结束
下一个intRec
终端使用
MsgBox(blnSectionFound.ToString)
端接头