I am writing a program that allows for people to answer simple questions on Capital Cities of the world. The main program works flawlessly, but I decided to add in a new function that records the user's answers to a question if they get it incorrect and posts it in an external file (named Incorrect.txt) and will then load the file to show the user when the Form "Leaderboard" is loaded.

The actual writing to the file is fine, but when I attempt to read it in from the file, I get the "Input past End of File Error". I have tried to fix this with the "end of file" statements, but it fails to work and according to my teachers, it should.

Here is the code for the read-in to the form.

Private Sub AnswerPaneLoad()
Dim Ansread As Variant
Dim Loading(2, 8) As String
Dim Readin2(2, 8) As String



Open "Incorrect.txt" For Input As #2

For Ansread = 1 To EOF(2)
Line Input #2, Loading(1, Ansread)
Line Input #2, Loading(2, Ansread)
AnswerPane.AddItem Loading(1, Ansread)
AnswerPane.AddItem Loading(2, Ansread)
Next
Close #2

Can anyone help?

Thanks.

Dani AI

Generated

Short diagnosis and what to change (summary of the replies from , and ): the error means your code is trying to read past the file end. EOF is a boolean check you must use immediately before an Input/Line Input; using EOF as a numeric loop bound or doing two blind reads per iteration will overshoot when the file has an odd number of lines or a trailing blank line. Microsoft documents this exact failure and recommends checking EOF right before each read. (learn.microsoft.com)

A simple, robust pattern: read the whole file into a string, split into lines, then process pairs safely. This avoids loop/EOF timing issues and makes it easy to handle a final single line. Example (VB6/VBA style):

' read entire file and parse pairs safely
Dim fnum As Integer, txt As String
Dim lines() As String, i As Long
fnum = FreeFile()
Open App.Path & "\Incorrect.txt" For Input As #fnum
If LOF(fnum) = 0 Then Close #fnum: Exit Sub
txt = Input$(LOF(fnum), #fnum)
Close #fnum

lines = Split(txt, vbCrLf)
For i = 0 To UBound(lines) Step 2
    Dim question As String, answer As String
    question = Trim(lines(i))
    If i + 1 <= UBound(lines) Then answer = Trim(lines(i + 1)) Else answer = ""
    If Len(question) > 0 Or Len(answer) > 0 Then
        AnswerPane.AddItem question
        AnswerPane.AddItem answer
    End If
Next i

Using FreeFile, LOF and Input$ is safer here (you avoid hard-coded file numbers and blind Line Input pairs). See the FreeFile/LOF/Input docs for details. (learn.microsoft.com)

Quick troubleshooting checklist:

  • Open the text file in Notepad and remove any trailing blank line or stray end-of-file characters.
  • If you must keep the two-Line-Input approach, check EOF immediately after the first Line Input before doing the second.
  • Declare counters as Integer/Long (not Variant) and use explicit array bounds (or dynamic arrays) so indexes match your data layout.

These small changes will stop the Input-past-EOF error and make the loader resilient.

Recommended Answers

All 3 Replies

Try...

Do While Not Eof (FileNumber)
  'do stuff here like reading in your file
Loop

Good Luck

I would use a do until loop. That way the code will only be executed only when there is a line in the text file. That should avoid the error that you mentioned.

Dim IntCounter as integer
Open "Incorrect.txt" For Input As #2
Do until EOF(2)
    Line Input #2, ....
    ...


Loop

Also, I would look at your code inside the loop: Loading(1, Ansread).

I would say that the first element of the array should be Loading(0, Ansread)

The Multi Dimensional arrays as you have them declared actually have 3 first elements (0, 1, 2) and 9 second elements (0, 1, 2, ....., 8). The first value of the Arrays declared as you have coded starts with 0: e.g. Loading (0, 0).

If you want to start with 1, you must be explicit in VB6. But I don't really have enough info from your code to determine if that is your actual problem. This is just a guess. So I couldn't really give you a solid answer here.

Dim strArray(1 to 2, 1 to 8) as String

Also, you might consider changing your code with regards to your variable Ansread declared as a Variant. You can probably get away with it; but since you're dealing with arrays that will increase with integer or long values, why use a variant variable: use an integer variable. I think you're just asking for trouble there.

But I'm assuming that Ansread has already been determined from code that you didn't include in your post and that it is an integer value.

Your issue may be with the multiple line input statements within the loop. I'd throw a breakpoint in and be sure the error isn't coming from the second input line. It may be that the first input line reads, then you acheive EOF, but then you try to do another read without checking EOF again. Blind reads are never good.

Also be sure that your file does not have a blank line at the end of all of your data. If this is the case, then there's your issue.

I am writing a program that allows for people to answer simple questions on Capital Cities of the world. The main program works flawlessly, but I decided to add in a new function that records the user's answers to a question if they get it incorrect and posts it in an external file (named Incorrect.txt) and will then load the file to show the user when the Form "Leaderboard" is loaded.

The actual writing to the file is fine, but when I attempt to read it in from the file, I get the "Input past End of File Error". I have tried to fix this with the "end of file" statements, but it fails to work and according to my teachers, it should.

Here is the code for the read-in to the form.

Private Sub AnswerPaneLoad()
Dim Ansread As Variant
Dim Loading(2, 8) As String
Dim Readin2(2, 8) As String



Open "Incorrect.txt" For Input As #2

For Ansread = 1 To EOF(2)
Line Input #2, Loading(1, Ansread)
Line Input #2, Loading(2, Ansread)
AnswerPane.AddItem Loading(1, Ansread)
AnswerPane.AddItem Loading(2, Ansread)
Next
Close #2

Can anyone help?

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.