[RESOLVED] Error past end of file
I have a Function that tracks uses of my program and every once in a while
I get an error Error #62 Input past end of file. It seems a value is not written to the file in some cases and the error occurs because the file is empty. Why does a value not get written to the in the FirstLoad part of the function ?
Code:
Function Countit()
Dim fnumber As Integer
Dim fNumber2 As Integer
Dim File1 As String
Dim File2 As String
Dim Value1 As String
Dim Value2 As String
Dim Count1 As String
Dim Count2 As String
On Error Goto e
File1 = gPathWithOutFolder & "\xpW32vst.syv"'path set in sub main
File2 = gPathWithOutFolder & "\xpW32sys.xyp"
fnumber = FreeFile()
80 If FileExists(File1) Then
90 Open File1 For Input As #fnumber
' Debug.Print FileLen(File1)
'If FileLen(File1) <> 0 Then
100 Line Input #fnumber, Value1 'error here on some systems
110 'Debug.Print File1
120 Close #fnumber
130 Else
140 GoTo FirstLoad
' End If '
150 End If
FirstLoad:
510 Open File1 For Output As #fnumber
520 Count1 = 1
530 Print #fnumber, Count1
540 Close #fnumber
550 Open File2 For Output As #fnumber
560 Count2 = 1
570 Print #fnumber, Count2
580 Close #fnumber
Re: Error past end of file
Try this instead:
Code:
Open File1 For Input As #fnumber
Do While Not Eof(fnumber)
Line Input #fnumber, Value1
'do something with Value1 here
Loop
Close #fnumber
Re: Error past end of file
Quote:
Originally Posted by
RhinoBull
Try this instead:
Code:
Open File1 For Input As #fnumber
Do While Not Eof(fnumber)
Line Input #fnumber, Value1
Loop
Close #fnumber
Thanks for the reply, your method looks better than mine for checking:
If FileExists(File1) And FileLen(File1) > 0 Then
However I am more concerned with how the file could be empty. The first time a user runs my app it does not exist. I have changed the Firstload file handling to
fnumber2 = FreeFile()
Open File2 For Output As #fnumber2
Count2 = 1
Print #fnumber2, Count2
Close #fnumber2
This error happens so seldom and i have been able to recreate it, now i can only wait
Re: Error past end of file
Reasons for failure? Many, including permissions, path doesn't exist, out of disk space, bad sector maybe, and several more. Error checking should be applied. If the file fails to write, should be getting an error, no?
Re: Error past end of file
Quote:
Originally Posted by
isnoend07
I have a Function that tracks uses of my program and every once in a while
I get an error Error #62 Input past end of file. It seems a value is not written to the file in some cases and the error occurs because the file is empty. Why does a value not get written to the in the FirstLoad part of the function ?
Code:
Function Countit()
Dim fnumber As Integer
Dim fNumber2 As Integer
Dim File1 As String
Dim File2 As String
Dim Value1 As String
Dim Value2 As String
Dim Count1 As String
Dim Count2 As String
On Error Goto e
File1 = gPathWithOutFolder & "\xpW32vst.syv"'path set in sub main
File2 = gPathWithOutFolder & "\xpW32sys.xyp"
fnumber = FreeFile()
80 If FileExists(File1) Then
90 Open File1 For Input As #fnumber
' Debug.Print FileLen(File1)
'If FileLen(File1) <> 0 Then
100 Line Input #fnumber, Value1 'error here on some systems
110 'Debug.Print File1
120 Close #fnumber
130 Else
140 GoTo FirstLoad
' End If '
150 End If
FirstLoad:
510 Open File1 For Output As #fnumber
520 Count1 = 1
530 Print #fnumber, Count1
540 Close #fnumber
550 Open File2 For Output As #fnumber
560 Count2 = 1
570 Print #fnumber, Count2
580 Close #fnumber
I'm not exactly sure what your code looks like now but in the above code you don't need (and shouldn't have) the GoTo FirstLoad line and the FirstLoad label.
Re: Error past end of file
Quote:
Originally Posted by
LaVolpe
Reasons for failure? Many, including permissions, path doesn't exist, out of disk space, bad sector maybe, and several more. Error checking should be applied. If the file fails to write, should be getting an error, no?
Thanks for the reply
yes it gets an error: "Endput past end of file" and upon testing i have determined the file is created, but is empty.
permissions & path is not the problem as other files are written before this function to the same folder location.
bad sector and out of disk space would be rare at the same location with every pc.
this error only occurs in about 1 in 75 installs. I have narrowed it down by
my app when ran the first time sends me an email at 7 different check points,besides any errors. The app gets installed about 20 times a day
Re: Error past end of file
Quote:
Originally Posted by
MartinLiss
I'm not exactly sure what your code looks like now but in the above code you don't need (and shouldn't have) the GoTo FirstLoad line and the FirstLoad label.
thanks for adding that
i have used line# and goto in many places without problems, but have seen
where this this bad practise. Why ?
Re: Error past end of file
That's the error you get when reading the file, correct? Do you get any errors while writing the file? If not checking, maybe you should.
Re: Error past end of file
Quote:
Originally Posted by
LaVolpe
That's the error you get when reading the file, correct? Do you get any errors while writing the file? If not checking, maybe you should.
yes that was the error when reading the file. I corrected that by adding the check for the file being empty eg:
If FileExists(File1) And FileLen(File1) > 0 Then
now i am trying to determine why the files is empty and correct it. the app creates the file, but does not write the value to the file and does not goto the error handler.
Re: Error past end of file
I also posted this question at experts exchange, the answer:
You're re-using "fnumber" for two different files...
I would either re-assign "fnumber" with FreeFile() before using it with the second file or change to fnumber2 instead.
So at line #39 you could do:
fnumber = FreeFile()
Or complete switch to "fnumber2":
fnumber2 = FreeFile()
Open File2 For Output As #fnumber2
Count2 = 1
Print #fnumber2, Count2
Close #fnumber2
Re: Error past end of file
Quote:
Originally Posted by
isnoend07
I also posted this question at experts exchange, the answer:
You're re-using "fnumber" for two different files...
I would either re-assign "fnumber" with FreeFile() before using it with the second file or change to fnumber2 instead.
So at line #39 you could do:
fnumber = FreeFile()
Or complete switch to "fnumber2":
fnumber2 = FreeFile()
Open File2 For Output As #fnumber2
Count2 = 1
Print #fnumber2, Count2
Close #fnumber2
Code:
FirstLoad:
505 fnumber = FreeFile()
510 Open File1 For Output As #fnumber
520 Count1 = 1
530 Print #fnumber, Count1
540 Close #fnumber
545 fnumber = FreeFile()
550 Open File2 For Output As #fnumber
560 Count2 = 1
570 Print #fnumber, Count2
580 Close #fnumber
Re: [RESOLVED] Error past end of file
Thanks everyone
I have created a new exe with reassigning freefIle. Now i will wait with my rabbits foot and four leaf clover
Re: Error past end of file
Quote:
Originally Posted by
isnoend07
thanks for adding that
i have used line# and goto in many places without problems, but have seen
where this this bad practise. Why ?
Because it can lead to spaghetti code and I've rarely ever seen cases where it's actually needed except of course for On Error GoTo.
Re: Error past end of file
isnoend07,
I just joined VBForums and found this old post of yours. I'm having the same issue that looks like you might have been having and wasn't sure if you could be of any help. The app that I've created is distributed to an OEM's customers and is used to save & load operation parameters for the app The app may run for months without a customer having an issue and then it will pop up with an error 62 Input past end of file. Quite a while back after listening to a report of a user that experienced the problem I now output the same data to a second file that can be recalled if the error 62 comes up to input the data from the 2nd file and recreate the first file. The problem I now have is it looks like it is also happening to the 2nd file. Both files are opened at the same time in the same procedure. Like you, this has been a very difficult thing to pin down as I only hear about it once every 3-4 months.
(Below is an example of my code, I'm using file numbers instead of FreeFile)
Open x.dat for Output as 1
Open x2.dat for Output as 2
Write #1, data, data, data
Write #2, data, data, data
Write #1, data, data, data
Write #2, data, data, data
etc
Close 1
Close 2
Thanks in advance for any ideas.