Re: File IO-I am defeated!




"PeterD" <peter2@xxxxxxxxxx> wrote
OK, I have what should be the simplest of code fragments that there
is. Open a file, read it, close:
<... snipped for brievity ...>

60 Open strPath For Input Lock Read As #nFile
70 strBuffer = Input(nFileLength, #nFile)
80 Close #nFile

Now, this is something that I'd teach in my Intro to Programming
class, and expect it to work 100% of the time.

I would not expect that to work 100% of the time and am surprised
that you would. You've opened the file, and locked out other apps
from reading it, why? If some other app does that, your code here
will fail on the Open command and has no error handler to handle
that condition.

I would expect that you should plan to lock writing to the file while
you are trying to read the data. And, I would expect that there should
be an error handler in place in case you are locked out of the file by
some other process.

As others indicated, the file contents may be causing your problem.
Specifically the EOF character will cause this error (Chr(26)).

Dim s As String
Dim f As Long, i As Long
Const test = "D:\temp\file.txt"

s = "test"
f = FreeFile
Open test For Output As f
Print #f, s
Close

Open test For Input As f
s = Input(LOF(f), f) ' it works...
Close

Debug.Print s

Open test For Binary As f
Put #f, 2, CByte(26)
Close

Open test For Input As f
s = Input(LOF(f), f) ' it errors
Close


.