Re: File IO-I am defeated!
- From: "Larry Serflaten" <serflaten@xxxxxxxxxxxxxx>
- Date: Wed, 27 Feb 2008 15:17:49 -0600
"PeterD" <peter2@xxxxxxxxxx> wrote
OK, I have what should be the simplest of code fragments that there<... snipped for brievity ...>
is. Open a file, read it, close:
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
.
- Follow-Ups:
- Re: File IO-I am defeated!
- From: PeterD
- Re: File IO-I am defeated!
- References:
- File IO-I am defeated!
- From: PeterD
- File IO-I am defeated!
- Prev by Date: Re: Loading Images into an ImageList Control
- Next by Date: Re: File IO-I am defeated!
- Previous by thread: RE: File IO-I am defeated!
- Next by thread: Re: File IO-I am defeated!
- Index(es):