我正在尝试读取随机访问文件,但我在第一个文件Error 5 (unable to read beyond end of the stream)
上收到以下错误。我不确定我在这里做错了什么,我该如何解决这个问题?如何正确读取VB.NET中的随机访问文件
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
其实,我不知道你是否能结合BinaryReader.ReadInt16等使用FileStream.Seek()?嗯.... – SSS
BitConverter类可能也有帮助:http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx – SSS
感谢您的详细评论,我绝对计划开始更新不久的将来,我一定会把你的建议考虑在内。由于我仍然只是一个初学者,我只是想了解并修复我目前的错误。你有什么想法,为什么我的原始错误说,它不能阅读超过流的末尾?我不确定我做错了什么? – Matthew