读取随机访问文件 [英] Reading Random Access Files
本文介绍了读取随机访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用VB2008.
我继承自VB6,一直在使用随机访问文件,这些文件对结构化数据使用起来非常简单.
所以我的代码如下:
I am using VB2008.
Inherited from VB6, I have been using random access files which are really simple to use with structured data.
So I have this code below:
Dim f As Integer = FreeFile(), m As s_MailingList
FileOpen(f, MailFile, OpenMode.Random, OpenAccess.ReadWrite,OpenShare.LockRead, Len(m))
MailK = CInt(LOF(f)) \ Len(m)
ReDim Mail(MailK)
For i as Integer = 0 To MailK
FileGet(f, Mail(i), i + 1)
Next i
FileClose(f)
它运行完美,但是我想使用.NET方法,这也可以运行:
it works perfectly, but I wanted to use .NET methods and this also works:
Dim fs As New FileStream(MailFile, FileMode.Open)
Using sr As New BinaryReader(fs)
Dim b() As Byte = sr.ReadBytes(416)
'' do work to get into structure
sr.Close()
End Using
我检查了第一行,它是正确的.我需要编写循环等.因此,我似乎基于Microsoft代码尝试了此操作,因为它似乎更简单:
I checked the first line and it is correct. I need to write loop etc. So I tried this based on Microsoft code as it seems simpler:
Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(MailFile)
Reader.TextFieldType = FileIO.FieldType.FixedWidth
Reader.SetFieldWidths(1, 40, 40, 40, 40, 40, 40, 20, 20, 20, 20, 20, 60, 5, 4, 1, 2, 1, 1)
Dim currentrow() As String
While Not Reader.EndOfData
Try
currentrow = Reader.ReadFields()
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox(ex.Message , "is not valid and will be skipped.")
End Try
End While
End Using
最后的代码无法读取该行. FieldWidths总计为416.
有任何想法吗?
The last code fails to read the line. The FieldWidths add up to 416.
Any ideas?
推荐答案
确定吗?
仅累加非零单位:1 + 5 + 4 + 1 + 2 + 1 +1.
你有16点吗?
因为我得到一个奇数...
Are you sure?
Add up just the non-zero units: 1 + 5 + 4 + 1 + 2 + 1 + 1.
Do you get 16?
Because I get an odd number...
我写了这似乎有用.
I wrote this which seems to work.
Public Class OpenRandomFiles
Private fs As FileStream, sr As BinaryReader
Friend Sub New(ByVal fPath As String)
fs = New FileStream(fPath, FileMode.Open, FileAccess.Read, FileShare.Read)
sr = New BinaryReader(fs)
End Sub
Friend Sub Dispose()
sr.Close()
fs.Dispose()
End Sub
#Region " Properties "
Private fCount As Integer
Friend ReadOnly Property Count() As Integer
Get
Return fCount
End Get
End Property
Private fStrings() As String
Friend ReadOnly Property Strings() As String()
Get
Return fStrings
End Get
End Property
Private fWidths(), wCount As Integer
Friend WriteOnly Property Widths() As Integer()
Set(ByVal value As Integer())
fWidths = value
wCount = UBound(fWidths)
ReDim fStrings(wCount)
Dim w As Integer = 0
For i As Integer = 0 To wCount
w += fWidths(i)
Next
fCount = CInt(fs.Length \ w - 1)
End Set
End Property
#End Region
Friend Function ReadLine() As Boolean
Try
For i As Integer = 0 To wCount
Dim b() As Byte = sr.ReadBytes(fWidths(i))
fStrings(i) = ""
For k as Integer = 0 To UBound(b)
fStrings(i) = Chr(b(k))
Next k
Next
Return False
Catch ex As Exception
Return True
End Try
End Function
End Class
像这样
It works like this
Dim l() As Integer = {1, 40, 40, 40, 40, 40, 40, 20, 20, 20, 20, 20, 60, 5, 4, 1, 2, 1, 1}
Dim f As New OpenRandomFiles(MailFile)
f.Widths = l
MailK = f.Count
ReDim Mail(MailK)
For i As Integer = 0 To MailK
f.ReadLine()
Dim s() As String = f.Strings
With Mail(i)
.Flag = Asc(s(0))
.Name = s(1)
.Spare1 = FnAsc(s(14))
.Spare2 = Asc(s(15))
End With
Next
f.Dispose()
首先,您声明的字段宽度为 415 而不是416.
您说最后的代码无法读取该行",但这是什么意思?它是否显示您的错误MessageBox或从未调用过ReadFields()
行,表明存在EndOfData
条件.
如果显示错误框,内部异常是否会提供有关原因的更精确信息?
Firstly, the field widths are 415 NOT 416 as you state.
You say "The last code fails to read the line" but what does that mean? Does it show your error MessageBox or is theReadFields()
line never called, indicating that there is anEndOfData
condition.
If your error box is displayed does the inner exception give more precise information about the cause?
这篇关于读取随机访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文