Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flatfile into Structure

Status
Not open for further replies.

realm174

Programmer
Jan 3, 2002
154
CA
Hello,

I am trying to read a single line text file into a user defined structure. The content is variable in length, but is always the same format. Here's the Structures definition I came up with:

Code:
    Structure basetype
        Dim dep As String
        Dim arr As String
        Dim alt As String
        Dim rf As String
        Dim fno As String
        Dim icao As String
    End Structure
    Structure rectype
        Dim ac As String
        Dim id As String
        Dim perc As String
        Dim dur As String
        Dim fr As String
        Dim bases As basetype()
    End Structure

So the first 5 fields in the text file are the first 5 fields in the structure "rectype", and following that, will be one or more "basetype". Everything is comma delimited.

Of course, I could manually parse through the one line, with a loop, until I reach the end of the line, but I was hoping there was an easier way than that. Anyone have a better idea? Point me in the right direction?

thanks!!


Cheers,

Realm174
 
Without more code it is hard to say, but if you have a string variable (which at some point you would have to) you can use .split(",") to split it into an array of strings.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorry, I guess I should have provided more details. The file is read into one string, that is correct, and I can certainly use .split(",") as you suggested, but then, I'll end up with one array that has each value as one individual item. What I am trying to do, I guess, is something similar to what I vaguely recall doing in the very old basic days where I would do something like (and don't quote me on the code, this is by memory)

Code:
   while not eof(1)
   input #1,field1,field2,field3,field4,field5,field6
   end while

or something along those lines... Come to think of it, I wonder if this still works in .NET. I should give that a try.

But my original question, however, was to find out if there is a better way to put the values in each field without having to loop through the entire array.

What I've been trying in the meantime (based on your suggestion) looks like this:

Code:
Sub doit
        Dim lines As String()
        Dim fields As String()
        Dim content As String = My.Computer.FileSystem.ReadAllText(filename)
        lines = content.Split(vbCrLf)
        For Each line As String In lines
            'process each line
            fields = line.Split(",")
            With myrec
                .ac = fields(0)
                .id = fields(1)
                .perc = fields(2)
                .dur = fields(3)
                .fr = fields(4)
                Dim poscount As Integer = 4
                Dim basescount As Integer = 0
                While poscount < UBound(fields)
                    With .bases(basescount)
                        .dep = fields(poscount + 1)
                        .arr = fields(poscount + 2)
                        .alt = fields(poscount + 3)
                        .rf = fields(poscount + 4)
                        .fno = fields(poscount + 5)
                        .icao = fields(poscount + 6)
                    End With
                    poscount = poscount + 6
                End While
            End With
        Next
End Sub

However, I'm having a problem. Each "myrec" contains one or more "bases". However, bases is never initialized to anything I guess, because when I try to assign a value to it, I get a NullReference exception error.

I do understand why I get that error, I just have no idea on how to fix it. How do I "new" an array variable within a structure?

Sorry if this seems obvious, I am not totally new at VB, but I am at creating structures and dealing with them...

Cheers,

Realm174
 
ok, I figured it out... or at least, I figured out a way that works just fine. I'll need to do some reading when I have a bit more time available.. :)



Cheers,

Realm174
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top