Re: File IO-I am defeated!
- From: PeterD <peter2@xxxxxxxxxx>
- Date: Wed, 27 Feb 2008 18:57:52 -0500
On Wed, 27 Feb 2008 15:17:49 -0600, "Larry Serflaten"
<serflaten@xxxxxxxxxxxxxx> wrote:
"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?
I wasn't clear... This is a temporary work file that my application
creates. I know it is text (not binary) because that is what I write.
I would be astounded if another application was reading my file! <bg>
If some other app does that, your code here
will fail on the Open command and has no error handler to handle
that condition.
There is an error handler just above the posted snippet.
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)).
That was a thought of mine, too...
.
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: Mike Williams
- Re: File IO-I am defeated!
- References:
- File IO-I am defeated!
- From: PeterD
- Re: File IO-I am defeated!
- From: Larry Serflaten
- File IO-I am defeated!
- Prev by Date: Re: Search and Replace
- Next by Date: Looking for a better way to do this
- Previous by thread: Re: File IO-I am defeated!
- Next by thread: Re: File IO-I am defeated!
- Index(es):
Relevant Pages
|
Loading