Whoa up!!!!!!!
We're talking about your textual file here, not your binary file.
How an &H20's appear in consecutive bytes within a numeric field. Seeing as
all your fields are strings, that's a contradiction in terms.
I assume that your "Deleted" records commant also applies to your binary
file and not your textual file.
By your definition thus far, a 'record in your file lookes like:
AAAAAAAA{0}BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB{0}
.... {0}
where {0} represents the NUL delimiter.
A record may also look like:
AAAA {0}BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB{0}
.... {0}
but will never look like:
AA AAAA{0}BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB{0}
.... {0}
The way I would be inclined to approach this is:
Dim _sb As New StringBuilder
Dim _br as New BinaryReader(File.Open(infileName, FileMode.Open))
While _br.PeekChar() <> -1
Dim _fields As String() =
Encoding.ASCII.GetString(_br.ReadBytes(_recsize)).TrimEnd(Char.MinValue).Split(Char.MinValue)
For _i As Integer = 0 To _fields.Length -1
_fields(_i) = _fields(_i).Trim
' Do whatever else needs doing with the 'field'
Next
_sb.AppendLine(String.Join(",", _fields))
Loop
_br.Close()
File.WriteAllText(outfilename, _sb.ToString)
_br.PeekChar() will return -1 when there are no more characters in the
stream.
Encoding.ASCII.GetString(_br.ReadBytes(_recsize)) reads the specified number
of the bytes from the stream, (advancing the stream pointer), and converts
it to a string.
TrimEnd(Char.MinValue) strips the final &H0 from the string.
Split(Char.MinValue) returns an array of strings from the string using &H0
as the split delimiter.
The inner loop is used to trim any whitespace from all the strings in the
array and makes each 'field' available for further processing. If the value
of the 'field' needs to modified it can be done here.
String.Join(",", _fields) create as comma-delimited string comprising all
the array elements.
The AppendLine method of the StringBuilder appends the supplied string and
automatically add a NewLine.
Now all you variables, (sA, sB, etc.) are not required, nor do you have to
worry about where a given 'field' starts or how long it is. Each 'field' is
determined by it's (0 based) position in the array, i.e., sA equates to
position 0, sB equates to position 1, etc.