Would like to know the cause of the error type or code
Pls help
Short summary and next steps (expanded from replies by , and ):
Runtime error 62 ("Input past end of file") means your code tried to read more data than exists in the file. That most often comes from (a) reading after EOF in a sequential/text loop, or (b) asking for a fixed number of bytes/characters from a file opened with the wrong mode (text/Input vs binary) so the read requests more data than available. (learn.microsoft.com)
If the file is binary, prefer Binary access and Get rather than Input. The pattern below reads the whole file safely into a byte array: it opens for Binary, uses LOF to get the size, allocates a buffer, then Get reads exactly that many bytes.
Dim fnum As Integer
Dim fileSize As Long
Dim buf() As Byte
fnum = FreeFile
Open fileName For Binary Access Read As #fnum
fileSize = LOF(fnum)
If fileSize > 0 Then
ReDim buf(0 To fileSize - 1)
Get #fnum, , buf
End If
Close #fnum Get reads bytes into the buffer and LOF returns the open file length; both are the correct building blocks for binary IO. (learn.microsoft.com)
Quick troubleshooting checklist:
This addresses the common causes raised in the thread and gives a small, safe pattern to replace the fragile Input/FileLen approach.
Jump to Post— WaltP 2,905Reading again after you already reached the end of the file.
Reading again after you already reached the end of the file.
What is the code that is throwing the mentioned error message ?
The most common causes are
Cause 1: you're trying to read before you reach end of file
Solution 1: try testing EOF (see example below)
Cause 2: you opened the file in a wrong mode
Solution 2: correct the mode you're opening this file (see example below)
Open "YOURFILE.TXT" For Input As #1
'To test EOF use EOF function, like this
While not EOF(1) Don't forget to change YOURFILE.TXT to the path/file name you can access and the 1 number to a position in memory that is not in use (try using FreeFile if you have doubt)
Open "YOURFILE.TXT" For Output As #1
'The line below will produce the error message you saw
Line Input #1, cTeste This one occurs cause you open the file in OUTPUT mode, but are trying to request a line, it's not permitted. Correct using INPUT mode instead.
Regards,
Sidnei
Actually, reading a file opened for input and using the input function with either LOF or FileLen could raise this error. Instead of opening for input you would want to open as binary. As an example... (adjust if you installed vb somewhere else...
Dim FName As String, FNumb As Integer, FCont As String
FName = "C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Misc\SECUR02B.ico"
FNumb = FreeFile
Open FName For Input As #FNumb
FCont = Input(FileLen(FName), #FNumb)
Close #FNumb Now as you will see when you run this code it will give you the error 62, input past end of file but if you adjust for input to for binary, there will be no problem...
Good Luck
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.