error-handling - 防止输入超出文件结尾错误

标签 error-handling vb6 runtime-error

我正在清理已故父亲的一些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出现EoF错误时可以跳到Line Input #4, adate行,代码将自行修复

我觉得修复可能很简单,我只是缺乏VB6和错误处理方面的经验。

最佳答案

您可以检查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      

关于error-handling - 防止输入超出文件结尾错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26468225/

相关文章:

c# - ASP.NET Web API堆栈跟踪不可用-使用ProblemDetails

file-upload - GraphQL 文件上传的拒绝和错误处理

javascript - 从压缩文件中捕获JavaScript错误

csv - 如何使用VB6写入CSV文件

csv - 如何在 VB6 中根据匹配值合并两个 CSV 文件

java - 在 @Application bean 中使用构造函数注入(inject)会导致循环引用

parsing - 错误恢复并解析Anltr4中的树

c# - 在 C# 中访问 VB6 字符串的最快方法

email - 错误处理-如果网络中断,则循环生成电子邮件的脚本

regex - VBA 宏上的运行时错误