Error reading an empty text file?

Using VB6

When I read an empty text file showing the error as input end of file end

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 if a text file is empty or not? If empty does not need to read the content, then it should read the content.

Need help on VB6 code

+3
source share
3 answers

You do it:

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

You should check 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, however, you have a logical error: ReadAll will read the entire file in memory. Therefore, subsequently calling ReadLine will not return anything. Either use ReadAll, or parse the input using string manipulations, or just call ReadLine. Do not use both options.

+8
source

, Stefan . .

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

(-), strTheData ". FileLen.

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

FileSystemObject: - , - - ? EDIT: , FileSystemObject.

+2

, .Size, :

set folder = fso.GetFolder(path)
for each file in folder.Files
  Response.Write file.Name & ": " & file.Size & "<br>"
Next
0
source

Source: https://habr.com/ru/post/1720438/


All Articles