gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:40:52 29 4
gpt4 key购买 nike

我正在清理已故父亲的一些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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com