Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input past end of file 62 error

Status
Not open for further replies.

tb2007

Programmer
Apr 2, 2005
33
YU
I have in my VB application code to read text file like this:

Dim n as integer
Dim Text as string

n = FreeFile()
Open File.txt For Input As n
Text = Input(LOF(n), n
Close n

In some cases I get error:
Input past end of file 62

After that error my application cannot work.

Could somebody help me with this?
 
Dim f As Integer
f = FreeFile()

Open "C:\file.txt" For Input As #f
While Not EOF(f)
' reads from 1st to the last line
Wend
Close (f)
 
But I need Input function because unlike the Input # statement, the Input function returns all of the characters it reads, including commas, carriage returns, linefeeds, quotation marks, and leading spaces.
My text in those files is formated like word file, so I need Input function to read the text. I cannot produce this error. If I can I would find way to handle it somehow.

 
If you have to use this method of reading the file then just use "Line Input" instead of "Input" every time you read from the file to put the entire line into a variable, and not just the first field in it. And don't forget to use EOF.
-Max
 
If you are trying to read the entire text file in to a variable, then try replacing your code with this...

Text = CreateObject("Scripting.FileSystemObject").OpenTextFile(File.txt, ForReading).ReadAll


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
You're right, it is evil. I would never let this get in to production. I would change it slightly.

I would add a reference to the 'Microsoft Scripting Runtime'

I would have code like this...

Dim FSO As Scripting.FileSystemObject

Set FSO = CreateObject("Scripting.FileSystemObject")
Text = FSO.OpenTextFile(File.txt, ForReading).ReadAll
Set FSO = Nothing

Or, better yet.

Code:
Public Function GetFileContents(ByVal FileName As String) As String
    
    Dim FSO As Scripting.FileSystemObject
        
    Set FSO = CreateObject("Scripting.FileSystemObject")
    GetFileContents = FSO.OpenTextFile(FileName, ForReading).ReadAll
    Set FSO = Nothing
    
End Function

And to use it, in the context of the original post...

Text = GetFileContents(File.txt)

*** Assuming the File.txt is a text box

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top