Re: File IO-I am defeated!



PeterD wrote:
OK, I have what should be the simplest of code fragments that there
is. Open a file, read it, close:


50 If funFileExists(strPath) Then ' file is really there.
55 nFile = FreeFile
60 Open strPath For Input Lock Read As #nFile
61 nFileLength = LOF(nFile)
63 If nFileLength > 0 Then ' File has data in it...
70 strBuffer = Input(nFileLength, #nFile)
73 End If
80 Close #nFile
90 End If

Now, this is something that I'd teach in my Intro to Programming
class, and expect it to work 100% of the time. The file's contents are
text, there is nothing special about the file. Usually the file is
created by the program dynamically, occasionally it will be an
existing file.

I'm getting err.number = 70, err.description = "Input past end of
file" for some users! This has *got* to be something so obvious that I
can't see it, but what????

BTW, I have no hair left, and am willing to post a picture proving it!


Interesting code. If the file is empty, you only have an empty string
to go on, so reading it anyway can do no harm. Here's what I use:



fh = FreeFile
Open sFn For Input Access Read Lock Write As fh
GetFile = Input$(LOF(fh), fh)
Close #fh


Doesn't really address the errors you're reporting, but I think Mike
has covered that. Binary data can contain "EOF" characters without
them having any such intrinsic meaning.



Bob
--
.



Relevant Pages

  • Re: File IO-I am defeated!
    ... 55 nFile = FreeFile ... 60 Open strPath For Input Lock Read As #nFile ...
    (microsoft.public.vb.general.discussion)
  • Re: File IO-I am defeated!
    ... 55 nFile = FreeFile ... 60 Open strPath For Input Lock Read As #nFile ...
    (microsoft.public.vb.general.discussion)
  • Re: Importing a text file
    ... a call to FreeFile, the officially correct way to get a file number. ... Dim nCounter As Long ... Dim nFile As Long ...
    (comp.lang.basic.visual.misc)
  • Re: Importing a text file
    ... a call to FreeFile, the officially correct way to get a file number. ... Dim nCounter As Long ... Dim nFile As Long ...
    (comp.lang.basic.visual.misc)
  • Re: Determining if a string is Unicode
    ... > Dim nFile As Long ... > Dim strLine As String ... > nFile = FreeFile ...
    (microsoft.public.vb.general.discussion)

Loading