如何读取空文本文件 [英] how to read empty text file

查看:111
本文介绍了如何读取空文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码可以从文本文件读取,但是如果我的文本文件没有文本,那么我的代码将无法工作并显示错误

我的代码:

I have a code like this to read from text file, but if my text file has no text then my code does not work and shows an error

MY CODE:

Dim sSourceFile, sLine As String
        Dim a As String
        sSourceFile = "E:\hash\WindowsApplication1\WindowsApplication1\bin\a.txt"
        FileOpen(100, sSourceFile, OpenMode.Input, OpenAccess.Read)

        sLine = LineInput(100)
        a = Mid(sLine, 1)
        ListBox1.Items.Add(a)
        While Not EOF(100)
            sLine = LineInput(100)
            a = Mid(sLine, 1)
            ListBox1.Items.Add(a)
        End While
        FileClose(100)
        IP_Server = ListBox1.Items(0)
        server = ListBox1.Items(1)
        DB = ListBox1.Items(2)
        User_Server = ListBox1.Items(3)
        Pass = ListBox1.Items(4)





如果文本文件为空,则会出现我的错误:

输入文件末尾.
对于行"sLine = LineInput(100)"显示此错误


tnx为您提供帮助





my error if text file is empty:

Input past end of file.
this error show for line " sLine = LineInput(100)"


tnx for yr help

推荐答案

为什么文件长度比预期的短两倍,为什么不问该怎么办?它包含一些垃圾怎么办?实际上,这与空文件完全相同.

您可以问自己,为什么首先允许读取文件中的零字节并进行修复,但这不是最重要的事情.

您应该始终期望任何文件都包含意外内容.您可以期望文件不可访问.这样,您无需检查任何特殊条件,例如文件是否为空或类似的东西.您无法使用文件预测所有可能的错误情况.任何人都可以访问该文件并处理或截断任意数量的字节.您看到的是合法的例外.捕获异常,获取异常信息(类型,消息),因为已知的异常类型将其包装在一些合理的说明文字中并呈现给用户.

—SA
Why you don''t ask what to do if a file is two times shorted than expected? What to do it it contains some garbage? Effectively, this is exactly the same as empty file.

You can ask yourself why do you allow to read zero bytes in the file in first place and fix it, but this is not the most important thing.

You should always expect that any file contains anything unexpected. You can expect that a file is inaccessible. In this way, you don''t need to check any special condition like if the file is empty or something like that. You cannot predict all possible error conditions with file. Anyone could get to the file and mangle or truncate any number of bytes. What you see is the legitimate exception. Catch the exception, get exception information (type, message), for know exception types wrap it in some reasonable explanation text and present to the user.

—SA


这篇关于如何读取空文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆