user3714887
user3714887

Reputation: 77

Error: Input past end of the file?

I am working on VB Script and I am trying to read the txt file and sore it in a array. I check for the number of lines and use that variable for the For loop.

I am getting an error Input past end of the file. I am not sure how to solve this problem.

looking forward for your help.

Thank you!!

Dim num As Integer
'Skip lines one by one
Do While objTextFile.AtEndOfStream <> True
    objTextFile.SkipLine ' or strTemp = txsInput.ReadLine
Loop

num = objTextFile.Line - 1


Dim para()
ReDim para(num)


For i = 1 To num

    para(i) = objTextFile.ReadLine

Next

Upvotes: 0

Views: 2086

Answers (2)

mconwell
mconwell

Reputation: 23

I've used the following style code which is fast for small files:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strList, ForReading)

strText = objTextFile.ReadAll
objTextFile.Close

arrList = Split(strText, vbCrLf)

Upvotes: 0

Guffa
Guffa

Reputation: 700302

For two reasons (the second coming intp play if you fix the first):

  • You have already read the file to the end. You would need to reset or reopen it.
  • You are always reading 125 lines, regardless of how many lines you found.

You can read the lines and put them in the array in one go:

Dim para()
Dim num As Integer = 0
Do While Not objTextFile.AtEndOfStream
  ReDim Preserve para(num)
  para(num) = txsInput.ReadLine
  num = num + 1
Loop

Note: Arrays are zero based, and the code above places the first line at index 0. If you place the data from index 1 and up (as in the original code) you leave the first item unused, and you have to keep skipping the first item when you use the array.

Edit:

I see that you changed 125 to num in the code, that would fix the second problem.

Upvotes: 2

Related Questions