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 最终会变成空(主要是在长时间停机或断电之后)。 当我在 Line Input #4, adate

处出现 EoF 错误时,如果我可以跳到 If Not SkipUpdate Then 行,代码将自行修复

我觉得修复可能非常简单,我只是缺乏 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/

相关文章:

django - Django中的错误响应

c# - 通过互操作将 VB6 对象传递给 .NET 对象?

soap - 什么是 WSDL 文件以及如何在 VB6/VBA 中使用它

c# - 当前上下文中不存在该名称 - grrr

ios - 在我创建的文件上运行打开时出现错误 13(权限被拒绝)(iOS)

oracle - Oracle Apex错误处理

c++ - C++::自定义异常,未捕获变量消息(<< 运算符)

vb6 - 如何在 VB6 项目中添加多个现有表单?

linux - x86 GNU 汇编程序奇怪的更改段错误

visual-studio-2010 - 使用 SURF 时出现运行时错误 Visual Studio 和 OpenCV 错误