tags:

views:

198

answers:

3

Using VB6

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

Code.

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.

Need VB6 code Help

A: 

If you just want to check file size, you could ask for .Size property directly, without opening that file:

set folder = fso.GetFolder(path)
for each file in folder.Files
  Response.Write file.Name & ": " & file.Size & "<br>"
Next
Rubens Farias
+2  A: 

You're doing this:

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

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

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.

Chris J
+2  A: 

If you just want to read a text file into a string, Stefan answered that on one of your questions in July. The answer is still the same.

strFilename = "C:\1.txt"
iFile = FreeFile
Open strFilename For Input As #iFile
strTheData = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
Close #iFile

If the file is empty (zero-length), strTheData will contain an empty string "". Alternatively you can check the length of a file with FileLen.

If FileLen("C:\1.txt") = 0 Then MsgBox "Empty file"

I recommend avoiding FileSystemObject: it's sometimes missing on user's machines - possibly because over zealous IT departments are scared of viruses? EDIT: in fact I've just heard from my colleagues about a user problem last week that turned out to be a FileSystemObject problem.

MarkJ