Input past end of file

KillGorack

New Member
Joined
Jan 23, 2006
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
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?

VBA Code:
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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
It's two files out of the 20 or so I'm parsing. I cannot find a difference between the files so far.
 
Upvote 0
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?
 

Attachments

  • Capture.PNG
    Capture.PNG
    55.3 KB · Views: 10
Upvote 0
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...

VBA 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...

VBA Code:
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(MyFileNum))
        MyFileText = Input(LOF(MyFileNum), MyFileNum)
    Close #MyFileNum
   
    Debug.Print MyFileText
   
End Sub

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

VBA Code:
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(MyFileNum))
        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!
 
Last edited:
Upvote 0
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...

VBA 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...

VBA Code:
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...

VBA Code:
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!

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

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top