Input past end of file

G

Guest

Hi,
I have following code:

Open strFileName For Binary Access Read Shared As intInputFileHandler
While (Not EOF(intInputFileHandler)
Line Input #intInputFileHandler, strBuff 'Read one line
..................
..................

Wend

It always read over the end of file. The error message is "Input past end of
file"

If I change open mode like this:
Open strFileName For Input Shared As intInputFileHandler

It won't read over the end of file anymore. But the input file isn't 100%
text file, sometime it has binary characters. Thus reading process may stop
before it reach the end of file because it find EOF character inside the file.

So, if the input file has multiple lines, but sometime it can have binary
characters, what is the best way to open and read the whole file line by line
?

Thanks
Quang
 
D

Dirk Goldgar

Tran Hong Quang said:
Hi,
I have following code:

Open strFileName For Binary Access Read Shared As intInputFileHandler
While (Not EOF(intInputFileHandler)
Line Input #intInputFileHandler, strBuff 'Read one line
..................
..................

Wend

It always read over the end of file. The error message is "Input past
end of file"

If I change open mode like this:
Open strFileName For Input Shared As intInputFileHandler

It won't read over the end of file anymore. But the input file isn't
100% text file, sometime it has binary characters. Thus reading
process may stop before it reach the end of file because it find EOF
character inside the file.

So, if the input file has multiple lines, but sometime it can have
binary characters, what is the best way to open and read the whole
file line by line ?

Thanks
Quang

Hi, Quang. I can suggest two basic approaches. If the file isn't
terribly large, you can read the whole file into a string variable in
one statement, then parse out the lines by using InStr() to locate
successive new-line markers -- either vbCrLf, or just vbCr, or maybe
just vbLf, depending on how the file was written. To read the whole
file at one go, you could use code like this:

' ...
Dim strFileData As String
' ...
Open strFileName For Binary Access _
Read Shared As intInputFileHandler

strFileData = Input(LOF(intInputFileHandler), intInputFileHandler)

' ... now parse the lines out of strFileData

An alternative, which you would use if the file is likely to be large,
is to use your own fixed-length buffer variable, read chunks of the file
sequentially into that buffer using the Get statement, and parse the
lines out of the buffer.
 
J

John Nurick

Hi Quang,

The first thing I'd try is using the FileSystemObject object and opening
the file as a TextStream rather than a binary file. Use
TextStream.ReadLine to read lines.

Otherwise, its necessary to know more about your file, the "binary
characters" it contains, and what you want to happen to them. If it's
possible for the "binary characters" to include CRLF pairs that do not
signal linebreaks you will certainly have to write custom code to parse
the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top