When end of record is start of next

VDufort

New member
Joined
Aug 7, 2008
Messages
1
Programming Experience
1-3
I am reading in a text file that is formatted as below:
VB.NET:
Expand Collapse Copy
Record1 Data
           More
           More
Record2 Data
Record3 Data
Record4 Data
           More
           More
           More
           More
           More
Record5 Data
           More

And need to get this:
VB.NET:
Expand Collapse Copy
Record1 Data        More        More
Record2 Data
Record3 Data
Record4 Data        More        More        More        More        More
Record5 Data        More

How would you do this? My ONLY challenge is that I don't have an end of record marker, aside from the beginning of the next record.
I need to retain the data lines until I hit a new record - then output the previous record and go on from there.

I have come across the concept of List Collection but am uncertain if that is the best way to proceed.

Thank you,
Vincent
 
Ahh, so I am not the only one that gets input files like this with no decent EndOfRecord identifier!

Anyway, my approach to these files is basically the following pesudo code (feel free to ask if more specifics are needed).

VB.NET:
Expand Collapse Copy
Dim myRecords as New List(Of Record) 'Where Record is a class setup to properly store the fields in my data file.
Dim myCurrentRecord as Record

Dim myInputStream as StreamReader(myInputFile)
While myInputStream.Peek <> -1
  Dim myInputLine as String = myInputStream.ReadLine
  if myInputLine.Contains("NewRecordIdentifier")
    
    if myCurrentRecord Is Nothing then
       myCurrentRecord=New Record    'myCurrentRecord initialized for the first time
    else
       myRecords.Add(myCurrentRecord)   'add the existing record to the list
       myCurrentRecord=Nothing              'destroy its current copy
       myCurrentRecord=New Record        'initialize its new copy
    endif 'end of the "is myCurrentRecord = Nothing"

   endif 'end of the "is this the first line of a new record"

   'All the code for handling the data in the lines goes here.

End While
'Dont forget the last myRecord is still in memory
myRecords.Add(myCurrentRecord)
myCurrentRecord=Nothing

'All the code to do whatever you do with all that input data goes here.


Again, its just the pseudo approch, feel free to ask if anythings unclear!
 
Haven't tested it out but this should be close enough to give you an idea.

VB.NET:
Expand Collapse Copy
        Dim sr As StreamReader
        Dim Line As String = String.Empty
        Dim Record As String = String.Empty
        Dim RecordsList As New List(Of String)

        sr = File.OpenText("C:\Test.txt")

        While sr.Peek <> -1
            Line = sr.ReadLine()
            If Line.Contains("NextRecordMarker") Then
                RecordsList.Add(Record)
                Record = String.Empty
            Else
                Record = Record & Line
            End If
        End While

Another approach may be to read in the entire file and split it into an array based on your record delimiter.

VB.NET:
Expand Collapse Copy
Dim Records() As String = sr.ReadToEnd().Replace(ControlChars.CrLf, String.Empty).Split("|")
 
You know.. I'd just open the file in a good text editor like ultraedit and:

replace all the CRLF with "" (nothing)
replace all the Record with CRLFRecord
 
Back
Top