# Input past end of file



## KillGorack (Dec 20, 2022)

Reading a text file, while it works most of the time Ive ran into a situation where it does not.

When running the code below I get a Input past the end of file error at the "strFileText = Input(LOF(iFile), iFile)" line

The files it opens exist, and are text files, is there anything I should be looking for?


```
Sub ASubRoutineOfSorts()

    h = ActiveWorkbook.Sheets("files").Cells(Rows.Count, 1).End(xlUp).Row
    Set DMOData = CreateObject("Scripting.Dictionary")
   
    Dim strFileName As String
    Dim strFileText As String
    Dim iFile As Integer: iFile = FreeFile
   
    For x = 2 To h

        strFileName = ThisWorkbook.Sheets("files").Cells(x, 1).Value
        If CheckFileExists(CStr(strFileName)) = True Then
            Open strFileName For Input As #iFile
            strFileText = Input(LOF(iFile), iFile)
            Close #iFile
           
           
           
            ' do stuff..
           
           
           
        End If

    Next x
   
End Sub
```


----------



## KillGorack (Dec 20, 2022)

It's two files out of the 20 or so I'm parsing. I cannot find a difference between the files so far.


----------



## KillGorack (Dec 20, 2022)

I think its a corruption of sorts. I have no idea .. upon further investigation I'm ranomly getting a line of text (Null characters)

Text files are computer generated.

See image attached when opened in notepad2. any chance of handling this in a way where the entire file can be read at once?


----------



## Domenic (Dec 20, 2022)

Here's what I think is happening.

While `LOF(iFile)` correctly returns the length of the file in bytes, the Input function, however, skips those Null characters.  And so when it reaches the end of the file, it tries to continue reading since the number of total bytes specified by the LOF function included those Null characters.  Hence the error message.

You can see for yourself that the Input function skips those Null characters by running the following code...


```
Sub test1()

    Dim MyFilename As String
    Dim MyChar As String
    
    MyFilename = "C:\Users\Domenic\Desktop\sample.txt" 'change the path and filename accordingly
    
    Open MyFilename For Input As #1    ' Open file.
        Do While Not EOF(1)    ' Loop until end of file.
            MyChar = Input(1, #1)    ' Get one character.
            Debug.Print MyChar    ' Print to the Immediate window.
        Loop
    Close #1    ' Close file.

End Sub
```

Interestingly enough, if you open the file in binary mode, and you initialize a string variable to the length of the file, the Input function doesn't skip those Null characters...


```
Sub test2()

    Dim MyFilename As String
    Dim MyFileText As String
    Dim MyFileNum As Long

    MyFilename = "C:\Users\Domenic\Desktop\sample.txt" 'change the path and filename accordingly
    
    MyFileNum = FreeFile()
    
     Open MyFilename For Binary Access Read As #MyFileNum
        MyFileText = Space$(LOF(1))
        MyFileText = Input(LOF(1), MyFileNum)
    Close #MyFileNum
    
    Debug.Print MyFileText
    
End Sub
```

However, I would probably use the Get function instead...


```
Sub test3()

    Dim MyFilename As String
    Dim MyFileText As String
    Dim MyFileNum As Long

    MyFilename = "C:\Users\Domenic\Desktop\sample.txt" 'change the path and filename accordingly
    
    MyFileNum = FreeFile()
    
     Open MyFilename For Binary Access Read As #MyFileNum
        MyFileText = Space$(LOF(1))
        Get #MyFileNum, , MyFileText
    Close #MyFileNum
    
    Debug.Print MyFileText
    
End Sub
```

Alternatively, you could use the FileSystemObject instead to read the entire file.

Hope this helps!


----------



## KillGorack (Dec 21, 2022)

Domenic said:


> Here's what I think is happening.
> 
> While `LOF(iFile)` correctly returns the length of the file in bytes, the Input function, however, skips those Null characters.  And so when it reaches the end of the file, it tries to continue reading since the number of total bytes specified by the LOF function included those Null characters.  Hence the error message.
> 
> ...



Thanks, the GET there is a new one for me. I'm putting that in my snippits.. It's appreciated!


----------

