• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

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)
                    ParseLine(strLine)

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.
 
0
sasllc
Asked:
sasllc
4 Solutions
 
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!
0
 
BrianGEFF719Commented:

'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))
                    ParseLine(strLine)
                Loop


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

-Brian
0
 
_agj_Commented:
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:
1.read 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 else...do 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...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BrianGEFF719Commented:
If they set the Option Base 1, they will be just fine.
0
 
BrianGEFF719Commented:
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.

-Brian
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...
0
 
atheekurrahmanCommented:
Hi,

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






Atheek
0
 
BrianGEFF719Commented:
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:
InStr(SearchIn,SearchFor)
Mid(SearchText,Start,Length)

ie:

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


-Brian
0
 
ArkCommented:
strContent = fs.ReadAll()
strLines = Split(strContent,vbCrLf)
For i=0 to Ubound(strLines)
   'Your staff with strLines(i)
Next i
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now