读取空白文本文件时出错? [英] Getting Error on while reading a blank text file?

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

问题描述

使用 VB6

当我读取空白文本文件时,显示错误为 Input Past end of file

When I Reading the blank text file, showing error as Input Past end of file

代码.

Dim fso As FileSystemObject
Dim TS As TextStream
Dim TempS As String
Dim Final As String
Set fso = New FileSystemObject
Set TS = fso.OpenTextFile(txtSourceDatabaseFile & "\" & FileName, ForReading)
Final = TS.ReadAll
Do Until TS.AtEndOfStream
    TempS = TS.ReadLine
    Final = Final & TempS & vbCrLf
Loop
TS.Close

如何检查文本文件是否为空?如果为空则不需要读取内容,否则应该读取内容.

How to check whether the text file is empty or not? If empty no need to read the contents, else it should read contents.

需要 VB6 代码帮助

Need VB6 code Help

推荐答案

你正在这样做:

Final = TS.ReadAll
Do Until TS.AtEndOfStream
    TempS = TS.ReadLine
    Final = Final & TempS & vbCrLf
Loop

您应该在调用 ReadAll 之前检查 AtEndOfStream,例如:

You should check for AtEndOfStream before calling ReadAll, something like:

If TS.AtEndOfStream Then
   Final = ""
Else
    Final = TS.ReadAll
    Do Until TS.AtEndOfStream
        TempS = TS.ReadLine
        Final = Final & TempS & vbCrLf
    Loop
End If

但是请注意,您在那里遇到了一个逻辑错误:ReadAll 会将整个文件读入内存.所以随后调用 ReadLine 将不会返回任何内容.要么使用 ReadAll 并通过字符串操作解析输入,要么专门调用 ReadLine.不要同时使用.

As a note though, you've got a logic error in there: ReadAll will read the entire file into memory. So subsequently calling ReadLine will return nothing. Either use ReadAll and parse the input with string manipulation, or call ReadLine exclusively. Don't use both.

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

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