-1

Ok. I need to store some records in a file namely data.dat. Records in the file are sorted by date values. Each block of record starts with its date value along with a $ sign to indicate that a new block of record starts here and ends with a "#" sign to indicate end of the record block.

A sample of a record block would be:

$22/08/2013
(data)
(data)
(data)
#

The file data.dat contains several blocks like this, how can I extract each block in the file storing each in an array using vb.net?

4

2 回答 2

0

在我看来,一个元组四重奏会为此做好准备。

Dim Record As New List(Of Tuple(Of DateTime, String, String, Integer))

然后可以通过它的项目编号访问每个字段:

Record(0).Item1
于 2013-09-30T14:48:22.667 回答
0

而不是一个数组,我会使用一个List(Of T). 您可以创建一个自定义类:

Class Record
    Public Property DateValue As DateTime
    Public Property Data As New List(Of String)
End Class

这是从文件初始化列表的可能循环:

Dim allData As New List(Of Record)
Dim currentRecord As Record = Nothing
Dim currentData As List(Of String) = Nothing
For Each line In File.ReadLines("data.dat")
    If line.StartsWith("$") Then
        Dim dt As DateTime
        If Date.TryParse(line.Substring(1), dt) Then
            currentRecord = New Record()
            currentRecord.DateValue = dt
            currentData = New List(Of String)
            currentRecord.Data = currentData
        End If
    ElseIf currentRecord IsNot Nothing Then
        If line.EndsWith("#") Then
            allData.Add(currentRecord)
        Else
            currentData.Add(line)
        End If
    End If
Next
于 2013-09-30T12:06:05.493 回答