2014-10-20 63 views
2

我正在清理一些已故父親的Visual Basic 6代碼,該代碼從氣象站獲取信息並將其植入網站的文件中。防止輸入過去文件錯誤結束

我想知道如何處理這段代碼的EoF錯誤。

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 

有時LastRun.txt最終會變空(主要是經過長時間的停機或停電)。 代碼將修復本身如果我能跳到If Not SkipUpdate Then線時,我有民間文藝誤差在Line Input #4, adate

我覺得好像修復可能是很簡單的,我只是缺乏與VB6和錯誤處理的經驗。

回答

2

您可以檢查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  
+0

非常感謝你,正是我一直在尋找。 – 2014-10-25 15:44:30

1

輸入過去文件的結束」時有發生......