Re: File IO-I am defeated!



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
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?

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

.



Relevant Pages

  • Re: File IO-I am defeated!
    ... and locked out other apps ... be an error handler in place in case you are locked out of the file by ... Dim f As Long, i As Long ... Open test For Output As f ...
    (microsoft.public.vb.general.discussion)
  • Re: default constraint
    ... Does the error handler fire for every property, ... > Dim t As ADOX.Table ... >> Public Sub SetDefault() ...
    (microsoft.public.vb.general.discussion)
  • Re: Error handler Not working a 2nd time
    ... Dim ABFileTo2 As String ... > On Error GoTo ErrHandler2a ... > Hey guys above is my code (Error Handler). ...
    (microsoft.public.excel.misc)
  • Re: default constraint
    ... Does the error handler fire for every property, ... > Dim t As ADOX.Table ... >> Public Sub SetDefault() ...
    (microsoft.public.sqlserver.programming)
  • Re: default constraint
    ... Does the error handler fire for every property, ... > Dim t As ADOX.Table ... >> Public Sub SetDefault() ...
    (microsoft.public.access.modulesdaovba)

Loading