Parsing Fixed length files

The following code will read the file passed into an array (OriginalLineContents) using the split function. This array is then processed in a loop to copy only the good elements of this array to a new array (LineContents). At this point, you have the values of your file in an array without any spaces. You now just need to reference the elements of the array that you are interested in.



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ParseFile("C:\Projects\Read Fixed Length File And Put In Array\Test.txt")

    End Sub

    Public Sub ParseFile(ByVal FileName As String)

        Dim OriginalLineContents() As String
        Dim LineContents() As String
        Dim i As Integer
        Dim x As Integer

        Dim SR As New System.IO.StreamReader(FileName)

        Do While SR.Peek <> -1

            OriginalLineContents = Split(SR.ReadLine)

            'Copy only valid values to new array
            x = 0
            Erase LineContents

            For i = 0 To OriginalLineContents.Length - 1

                If OriginalLineContents(i).Length > 0 Then

                    ReDim Preserve LineContents(x)

                    LineContents(x) = OriginalLineContents(i)

                    x += 1

                End If

            Next

            'At this point you can do what you need to do with the contents of the LineContents array.

        Loop

        SR.Close()
        SR = Nothing

    End Sub