Glenn L Vaughn
Glenn L Vaughn

Reputation: 25

Preventing Input past end of file error

I am cleaning up some of my late father's Visual Basic 6 code, which takes info from a weather station and plants it in files for a website.

I was wondering how I can deal with an EoF error for this code.

 Open "LastRun.txt" For Input As #4
    Line Input #4, adate
    adate = Trim(adate)
    flds = Split(adate, "/")
    If Len(flds(0)) = 1 Then flds(0) = "0" & flds(0)
    If Len(flds(1)) = 1 Then flds(1) = "0" & flds(1)
    thismonth = Trim(flds(2)) & "-" & Trim(flds(0))
    Close 4
    If Not SkipUpdate Then
        Open "cvtbmp.sh" For Output As #3
        Print #3, "cd /history" & vbLf;
        For i = 1 To lastfile
            aline = files(i)
            oline = "/usr/local/bin/convert -quality 40 " & aline & " " & Replace(aline, ".bmp", ".jpg")
            Print #3, oline & vbLf;
        Next
        Open "LastRun.txt" For Output As #4
        Print #4, Date
        Close

Occasionally the LastRun.txt ends up empty (mainly after a long period of downtime or power outages). The code will fix itself if I can skip to the If Not SkipUpdate Then Line when I have the EoF error at Line Input #4, adate

I feel as though the fix might be very simple, I just lack experience with VB6 and error handling.

Upvotes: 2

Views: 5378

Answers (2)

Denshio
Denshio

Reputation: 11

Input past end of file" sometimes happen when you use print instead of write...

Upvotes: 1

C-Pound Guru
C-Pound Guru

Reputation: 16368

You can check for EOF:

Open "LastRun.txt" For Input As #4
If Not EOF(4) Then
    Line Input #4, adate
    adate = Trim(adate)
    flds = Split(adate, "/")
    If Len(flds(0)) = 1 Then flds(0) = "0" & flds(0)
    If Len(flds(1)) = 1 Then flds(1) = "0" & flds(1)
    thismonth = Trim(flds(2)) & "-" & Trim(flds(0))
End If
Close 4
If Not SkipUpdate Then
    Open "cvtbmp.sh" For Output As #3
    Print #3, "cd /history" & vbLf;
    For i = 1 To lastfile
        aline = files(i)
        oline = "/usr/local/bin/convert -quality 40 " & aline & " " & Replace(aline, ".bmp", ".jpg")
        Print #3, oline & vbLf;
    Next
    Open "LastRun.txt" For Output As #4
    Print #4, Date
    Close      

Upvotes: 2

Related Questions