How to "see" a CrLf at the end of a text record?

I have a simple program that someone helped me write--thus I don't completely understand it--that reads in text records and outputs excel records.  Up until now, I have been reading in simple text records which were all 12 bytes long, so I'm using this code to read them in:

                ' Open the stream and read it back.
                Dim fs As FileStream

                fs = File.Open(strSourceFile, FileMode.Open, FileAccess.Read)

                'record length plus 1 here
                Dim b(14) As Byte
                Dim temp As UTF8Encoding = New UTF8Encoding(True)

                Dim strLine As String
                Do While fs.Read(b, 0, b.Length) > 0
                    strLine = temp.GetString(b)

As you can see, we are dimensiong 'b' as 14, which takes into consideration the 12 byte record plus the CrLf at the end of the record.

The problem is that I now need to be able to read in variable length records; some will be 6, while other are 12.  Both record types will end in CrLf.

Is there a simple way to build my string by reading data until I come to the CrLf, rather than having to use a fixed length as shown above?  I need an example that a rookie can understand, if possible.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sasllcAuthor Commented:
A couple of comments:

--The line that says 'record length plus 1 here' should actually say 'plus 2', because that's what we always did when the text records were 12 characters long: we dim'd b to 14 to account for the 2 byte CrLf at the end.

--The string that the data is going into is 'strLine', which I'm using further down in the program to break out data fields from the 12 byte text record...but that's probably obvious to you experts!

'Keep it at 14 (12 + Carriage Return + Line Feed)
'Trim it down from there

                ' Open the stream and read it back.
                Dim fs As FileStream
                fs = File.Open(strSourceFile, FileMode.Open, FileAccess.Read)
                Dim b(14) As Byte
                Dim temp As UTF8Encoding = New UTF8Encoding(True)

                Dim strLine As String
                Do While fs.Read(b, 0, b.Length) > 0
                    strLine = trim(temp.GetString(b))
                    strLine = Left(strLine, InStr(strLine, vbLf))

basically what that will do is it will read 14bytes regardless, it will trim off everything after the CRLF.

personally i have doubts as to whether the earlier code was working fine...

coz if u define the array as b(14) as byte, it actually has 0-14 ie. 15 bytes...

jus check up if b(13) works fine for your case..

the logic for the new stuff could jus be as: in 6+2=8 bytes.
2. check if the last characters are vcrlf
3. if so, use the same array to convert to string
4.else, read in 14-8=6bytes more...append to this byte array.
5. convert to string..

Can send a working code soon...

but i have a feeling there might be other options like:
1. do a filestream read with delimiter specified...need to check that.
2. just read in the entire file..and directly convert to string...thus we would have proper text version of the file with eeach record separated by crlfs....this can be processed in two ways
either this can be directly pushd into excel to generate rows...
or a Split() with vbcrlf as delimiter...this will generate an array of the records u are seeking to get...
however, if the file size is too big....might not be best...
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

If they set the Option Base 1, they will be just fine.
Sasslc: just add this line to the declartions section of your form.

Option Base 1

that will take care of your array starting at 0.

sasllcAuthor Commented:
The only one I understand, with my limited knowledge, is where agj suggests that I read in the first 8 bytes, and if that string does not end in crlf, then read in 6 more bytes.

But specifically, how would I do that?  I'm still not clear on exactly how my code knows to read in 14 bytes at this time, i.e. which command is telling it that?

And more important: how would I rewrite those lines of code to read in 8 bytes, check for crlf in 7 and 8, and then go on to read 6 more bytes if I did not find a crlf in 7 and 8?

I tried to find a way to implement Brian's suggestion about Option Base 1, but I can't find a place in my code that will accept that command without errors, and I don't know what this means: "that will take care of your array starting at 0."

Sorry I need it so simple, but...

What about checking for CrLf using InStr and then get the string using SubStr

SubStr isnt a function, but you can use Mid() that does the same thing as a SubStr Function. InStr Is a function however. VB is stupid, they should have gone with SubStr like everyone else in the world, but they picked Mid().

Saslic, the above functions can be used like so:


Dim position As Integer
Dim Text As String
Dim newText As String
Text = "this is an example"
position = InStr(Text, "e") 'will return first instance of i
newText = Mid(Text, position, 5) 'will return "examp"
MsgBox newText


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
strContent = fs.ReadAll()
strLines = Split(strContent,vbCrLf)
For i=0 to Ubound(strLines)
   'Your staff with strLines(i)
Next i
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.