Results 1 to 9 of 9

Thread: Reading a file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    At my computer
    Posts
    187

    Reading a file

    Howdy!

    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?

    Puzzled Allen :eek
    - Visual Basic 6.0
    - Windows XP Home

  2. #2
    Junior Member
    Join Date
    Jun 2001
    Posts
    28
    I am not quite shure that in understand your problem, but you fould try to use a rich textbox to view the file. ex:

    richtextbox1.filename = App.Path & "\Chris's Logbook.txt"

    of course, I am a new programmer, so this might not work. Good Luck

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    At my computer
    Posts
    187

    Re: <?>

    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.
    - Visual Basic 6.0
    - Windows XP Home

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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.
    "It's cold gin time again ..."

    Check out my website here.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    At my computer
    Posts
    187
    Ah yes, that could be.... Attached is the file.

    Thanks for helping....

    - Allen
    Attached Files Attached Files
    - Visual Basic 6.0
    - Windows XP Home

  6. #6
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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.
    "It's cold gin time again ..."

    Check out my website here.

  7. #7
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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
    "It's cold gin time again ..."

    Check out my website here.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    At my computer
    Posts
    187

    Thanks

    That works perfectly - thanks a lot, man. I wouldn't have thought of that.... Perhaps I can do a favor for you one day.

    Thanks again,

    Allen
    - Visual Basic 6.0
    - Windows XP Home

  9. #9
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    No problem, Allen. Enjoy.
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width