Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
196 views
Welcome To Ask or Share your Answers For Others

1 Answer

Instead of an array i would use a List(Of T). You could create a custom class:

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

Here's a possible loop to initialize the list from your file:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...