FileStream.Read leaves out last line of data

G

Guest

I have a stituation which requires me to read each byte in a text file. Each
line has 473 characters of text and then a line feed. Everything works great
except for the fact that the last line, despite having it's own line feed
(chr(10)) is not being read in. What could be causing this?

When I use a stream reader, it picks up all the lines, however I cannot use
a streamreader because there are not ASCII characters in the file that cannot
be left out (the streamreader drops those characters.) Why does the
FileStream.Read method leave out the last line?
 
H

Herfried K. Wagner [MVP]

mfm said:
I have a stituation which requires me to read each byte in a text file.
Each
line has 473 characters of text and then a line feed. Everything works
great
except for the fact that the last line, despite having it's own line feed
(chr(10)) is not being read in. What could be causing this?

Post your code.
When I use a stream reader, it picks up all the lines, however I cannot
use
a streamreader because there are not ASCII characters in the file that
cannot
be left out (the streamreader drops those characters.)

Use 'StreamReader' with the appropriate encoding ('System.Text.Encoding.*').
 
G

Guest

Here is my code. I left out the declarations. everytime I see a line feed is
a new record. The last line does not get imported:

ReDim byteArray(CInt(inputStream.Length) - 1)
inputStream.Read(byteArray, 0, CInt(inputStream.Length) - 1)
inputStream.Close()

For i = LBound(byteArray) To UBound(byteArray)
inputString &= Chr(byteArray(i))
If byteArray(i) = 10 Then
line = inputString
lineCounter = lineCounter + 1

Values(0) = Left$(line, 2)
Values(1) = Mid$(line, 3, 20)
Values(2) = Mid$(line, 23, 6)

inputString = ""
End If
Next

Import = lineCounter

g_sTimeEnd = Now.ToString
 
G

Guest

mfm said:
Here is my code. I left out the declarations. everytime I see a line feed is
a new record. The last line does not get imported:

ReDim byteArray(CInt(inputStream.Length) - 1)
inputStream.Read(byteArray, 0, CInt(inputStream.Length) - 1)

The Read method returns the actual number of bytes read. That isn't
always the number of bytes that you requested, so you have to repeat the
read until you get all the bytes.

If you ignore the return value from the Read method, only part of the
array might be filled with data, and you don't know how much data you have.
inputStream.Close()

For i = LBound(byteArray) To UBound(byteArray)
inputString &= Chr(byteArray(i))

A text file doesn't just contain bytes that represent character codes.
You have to decode the file properly using the same encoding that was
used to create the file, or you are going to loose characters.

Also, using &= for every single character means that you will be
creating a huge number of string object. To read a single line of 473
characters, you will be creating 473 strings, using more than 100 kb of
memory.
If byteArray(i) = 10 Then

A line break is not a single characters. It consists of the two
characters carriage return (13) and line feed (10).
line = inputString
lineCounter = lineCounter + 1

Values(0) = Left$(line, 2)
Values(1) = Mid$(line, 3, 20)
Values(2) = Mid$(line, 23, 6)

inputString = ""
End If
Next

Import = lineCounter

g_sTimeEnd = Now.ToString

Use the stream reader to read the file.

It does everything right that you don't. ;)
 
G

Guest

I appreciate your response. However a streamreader is not an option because
I have some characters that the stream reader ignores. They are the spanish
accented a, e ,i , o, u, and n. Because this is a fixed width file, it
messes up my entire import if I use the streamreader.
 
A

Armin Zingler

mfm said:
I appreciate your response. However a streamreader is not an option
because I have some characters that the stream reader ignores. They
are the spanish accented a, e ,i , o, u, and n. Because this is a
fixed width file, it messes up my entire import if I use the
streamreader.

As Herfried mentioned, use the appropriate Encoding when using the
StreamReader. Then, all characters should be read correctly. You can pass
the Encoding to the StreamReader's constructor. For example,
System.Text.Encoding.Default.


Armin
 
G

Guest

Thanks for your help (all of you.) This solution worked:

Dim sr As New StreamReader(stream, System.Text.Encoding.Default)

This picks up ALL of the characters (even the spanish accents) and leaves
off no lines.
 

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