I have created a large application which preforms many functions. One of these functions is to read log files created by another program.
Here is a sample log file: (copy into notepad for the example)
Code:
Chris's Logbook
FS2000
DATE AIRCRAFT MODEL TO/FROM/REMARKS DAY NIGHT INST. TOTAL
8/2/2000 CESSNA C152 Full CC-KAA turns,climbs,descents 1.0 0.0 0.0 1.0
8/7/2000 CESSNA C152 Full CC-KAA cfg changes,stalls,T&G 1.1 0.0 0.0 1.1
8/8/2000 CESSNA C152 Full CC-KAA steep turns,stalls,T&G 1.5 0.0 0.0 1.5
8/11/2000 CESSNA C152 Full CC-KAA Instrument work 1.2 0.0 0.5 1.2
8/13/2000 CESSNA C152 Full CC-KAA Instrument work 1.5 0.0 0.5 1.5
8/14/2000 CESSNA C152 Full CC-KAA T&G at BIL 0.8 0.0 0.0 0.8
8/17/2000 CESSNA C152 Full CC-KAA Instrument Work 1.4 0.0 0.5 1.4
8/21/2000 CESSNA C152 Full CC-KAA T&G at BIL 0.9 0.0 0.0 0.9
8/28/2000 CESSNA C152 Full CC-KAA T&G at BIL 0.8 0.0 0.0 0.8
9/10/2000 CESSNA C152 Full CC-KAA T&G at BIL 0.8 0.0 0.0 0.8
9/15/2000 CESSNA C152 Full CC-KAA FIRST SOLO 1.2 0.0 0.0 1.2
9/17/2000 CESSNA C152 Full CC-KAA Supervised solo at BIL 1.2 0.0 0.0 1.2
9/27/2000 CESSNA C152 Full CC-KAA Solo at BIL 0.6 0.0 0.0 0.6
10/17/2000 CESSNA C152 Full CC-KAA emergency procedures 0.6 0.0 0.0 0.6
10/21/2000 CESSNA C152 Full CC-KAA Solo north practice area 0.8 0.0 0.0 0.8
10/22/2000 CESSNA C152 Full CC-KAA Solo north practice area 0.7 0.0 0.0 0.7
10/28/2000 CESSNA C152 Full CC-KAA Solo north practice area 0.8 0.0 0.0 0.8
12/2/2000 CESSNA C152 Full CC-KAA Dual cross country 2.1 0.0 0.0 2.1
12/22/2000 CESSNA C152 Full CC-KAA Instrument work 1.1 0.0 0.5 1.1
1/20/2001 CESSNA C152 Full CC-KAA T&G at BIL 0.5 0.0 0.0 0.5
2/10/2001 CESSNA C152 Full CC-KAA Solo cross country 2.3 0.0 0.0 2.3
2/18/2001 CESSNA C152 Full CC-KAA soft field landings 1.2 0.0 0.0 1.2
3/25/2001 CESSNA C152 Full CC-KAA T&G at BIL 0.8 0.0 0.0 0.8
3/31/2001 CESSNA C152 Full CC-KAA Solo Cross Country 2.9 0.0 0.0 2.9
4/7/2001 CESSNA C152 Full CC-KAA T&G at Laurel 1.1 0.0 0.0 1.1
4/14/2001 CESSNA C152 Full CC-KAA T&G at BIL crosswind 0.7 0.0 0.0 0.7
5/12/2001 CESSNA C152 Full CC-KAA Solo North Practice Area 1.2 0.0 0.0 1.2
6/2/2001 CESSNA C152 Full CC-KAA Solo north practice area 1.1 0.0 0.0 1.1
TOTALS 31.9 0.0 2.0 31.9
In case you didn't guess, this is a Microsoft Flight Simulator 2000 Logbook file.
I'm having trouble reading the file. Here is the code I have:
Code:
On Error GoTo ErrHandler
Open App.Path & "\Chris's Logbook.txt" For Input As #1
Line Input #1, FileTitle
Line Input #1, BlankLine
Line Input #1, FileFormat
Line Input #1, sHeader
Line Input #1, BlankLine
Do 'Start a loop which will make an error when the file is done
Line Input #1, sLine
sData = sData & "~<!~>" & sLine 'Put the data into a string with a wierd code between each line
Text1.Text = Text1.Text & vbCrLf vbCrLf & sLine 'Put it into a textbox
Loop
Close #1
Done:
'Here we would split up the array and put it in a listview - I have 'that code, but it is not important for this example.
If FileFormat <> "FS2000" Then
MsgBox "This file is from a version of Flight Simulator other than 2000 or is not a valid logbook file. While FSLogbook may still read the file, some errors may occur.", vbInformation
End If
Me.Caption = "Log Book - " & FileTitle
Exit Sub
ErrHandler:
Select Case Err.Number
Case 62
Resume Done
Case Else
MsgBox "Runtime error " & Err.Number & " - " & Err.Description & ".", vbCritical
End Select
Exit Sub
OK, the idea is to take sHeader:
Code:
DATE AIRCRAFT MODEL TO/FROM/REMARKS DAY NIGHT INST. TOTAL
..and use Instr() to find the positions of "AIRCRAFT MODEL" etc, and then count that many spaces over in the data lines to extract each set of data. If that doesn't make sense, forget it, it doesn't work, because when LineInput reads the data in, it takes out all of the repeating spaces!!
So how the heck can I read this file in? How does Notepad do it?
Originally posted by HeSaidJoe Is your file a constant..ie are there x amount of spaces between each reported data or do the number of spaces change as well?
8/2/2000 CESSNA C152 Full CC-KAA turns,climbs,descents 1.0 0.0 0.0 1.0
When looking at the other log files I have, it does appear to be fixed, ie, the Aircraft model on all entries starts at position 11 on each line in all the files. How does this help?
The problem, to paraphrase, is that this:
Code:
a a
is read by LineInput as this:
Code:
a a
This makes it impossible to count the spaces to find the data. Yet Notepad can read it correctly....
I may try a RichTextBox - but I'd like to keep this app's size down, I'm already using the Common Controls (listbox) and the Inet control and it's going to be pretty big as it is. I'll use that as a last resort.
- Allen
Last edited by Allen Schoessler; Jun 9th, 2001 at 10:42 PM.
Hi Allen. What may be happening is that there are embedded Tab characters (Chr$(9)) in the file that are automatically expanded in Notepad. I am assuming that if you could be sure which position each item started in, you could extract the data you need from each line using the VB string functions (Mid, Left, Instr, etc.). If you don't mind, make that data file available for downloading so I can take a look at it; maybe we can figure something out.
OK - I found the problem (but not the solution yet). The problem is that there are Null characters in the file (ASCII 0). I found this out by looking at the file with DOS Debug. Notepad and another editor I have seem to convert these nulls to spaces when reading it in, however the VB Input statement does not do this. I tried doing a Replace on the string variable after the Line Input, but it seemed to be too late - VB already trimmed the string of the Null characters. My next move would be to read the whole file into a string in binary mode, try the Replace on that string (convert Ascii 0 to space), then write the file back out. Reading it back in as a regular Input file should then work. I'll give this a shot when I have a chance, meanwhile, the family is beckoning me to join them watching the basketball game, so I gotta go. I'll check back soon.
OK, I'm back (after a disappointing loss by the Sixers). Anyway, my theory was correct. Try the code below to verify. Put a big, wide Textbox on your form with MultiLine = True. Then put a command button with this code behind it:
Code:
Private Sub Command1_Click()
Dim strFile As String
Dim strLine As String
' I downloaded your file to the desktop ...
Open "C:\windows\desktop\chris's logbook.txt" For Binary As #1
strFile = Input(LOF(1), 1)
Close #1
strFile = Replace(strFile, vbNullChar, " ")
Open "C:\windows\desktop\newlogbook.txt" For Binary As #1
Put #1, , strFile
Close #1
Open "C:\windows\desktop\newlogbook.txt" For Input As #1
Text1.Text = ""
Do Until EOF(1)
Line Input #1, strLine
' Do the processing you set out to do in here ...
Text1.Text = Text1.Text & vbNewLine & strLine
Loop
Close #1
End Sub