Link to home
Start Free TrialLog in
Avatar of LJ083198
LJ083198

asked on

reading a file

I have a file that I need to input and reformat.  I have declared public types for the layout of the file(ex: mypublictype).  the file is fixed length, not delimited.  Once I have declared the type, I open the file for input and get a record.  What I am presently doing is defining each field using mid statements.  What I would like to do is have a command that say Input #1, mypublictype
and this command will break the record into fields based on my type declaration.  What I have found is that I can't do this because my file is not comma delimited.  Is there any way around this?  I can submit code if this is confusing.
ASKER CERTIFIED SOLUTION
Avatar of vettranger
vettranger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a sample that works:

Private Type mypublictype
    a As String * 4
    b As String * 6
    c As String * 10
    fill As String * 2
End Type
Private d As mypublictype
Private e As mypublictype

Private Sub Command1_Click()
    MsgBox d.a & vbCrLf & d.b & vbCrLf & d.c
    MsgBox e.a & vbCrLf & e.b & vbCrLf & e.c
   
End Sub

Private Sub Form_Load()
    Open "c:\vbapps\a.txt" For Binary As #1
    Get #1, Len(d) * 0 + 1, d
    Get #1, Len(e) * 1 + 1, e
    Close #1
End Sub

Form_Load read the forst two records of a fixed length file. Command1_Click displays them, you may want to loop until you have read all the records.

Note: in my Type I have a fill field because i seperated my records with a CRLF in the text file. Vettranger didn't. Vettranger: you beat me to the answer again!
Avatar of vettranger
vettranger

ventond, the filler method in the case of a CRLF on the end of the record is a good technique, I'm glad you included that.
Here is a sample that works:

Private Type mypublictype
    a As String * 4
    b As String * 6
    c As String * 10
    fill As String * 2
End Type
Private d As mypublictype
Private e As mypublictype

Private Sub Command1_Click()
    MsgBox d.a & vbCrLf & d.b & vbCrLf & d.c
    MsgBox e.a & vbCrLf & e.b & vbCrLf & e.c
   
End Sub

Private Sub Form_Load()
    Open "c:\vbapps\a.txt" For Binary As #1
    Get #1, Len(d) * 0 + 1, d
    Get #1, Len(e) * 1 + 1, e
    Close #1
End Sub

Form_Load read the forst two records of a fixed length file. Command1_Click displays them, you may want to loop until you have read all the records.

Note: in my Type I have a fill field because i seperated my records with a CRLF in the text file. Vettranger didn't. Vettranger: you beat me to the answer again!