0
votes

I'm parsing in a flat file by looping through and reading lines with a Streamreader.

Everything works well but a requirement changed where the field at the end of each record became optional. We validate the length of each line against the definition to determine if we should suspend that file as incorrectly formatted.

This lead to the discovery that Streamreader.ReadLine will trim any trailing spaces after the last character and before the newline.

Consider the following example with the numbers replaced by spaces:

BOB JONES 12345\n BOB JONES \n

The streamreader will, with both ReadLine and ReadToEnd store ignore those spaces. Here is the result in memory:

Readline:

"BOB JONES 12345" "BOB JONES"

ReadToEnd:

"BOB JONES 12345" & vbrclf & "Bob Jones"

Same deal with Readblock and then copying the buffer results into a string.

I will take a different approach to validating the length of the record since the ending date field is optional, but my question is why is the Streamreader dropping those ending spaces? And how would I read them in if I needed to?

1
I was extremely surprised by this behaviour so tested it. At least by default, for me, StreamReader.ReadLine does NOT do this. Are you sure there is not something else in your code trimming? Are you able to reproduce the behaviour in a small code sample? Can you confirm the .Net version you are using? - tolanj

1 Answers

0
votes

The StreamReader will not trim white-spaces which you can easily see with a sample program like this

Imports System.IO

Module Module1

    Sub Main()
        Dim sr As StreamReader = New StreamReader("SampleTextFile.txt")
        Dim text As String = sr.ReadToEnd

        Console.WriteLine("Original text")
        Console.WriteLine(text)
        Console.WriteLine()

        Console.WriteLine("White-spaces as .")
        Console.WriteLine(text.Replace(" ", "."))
        Console.WriteLine()

        Console.ReadKey()
    End Sub

End Module

and a corresponding SampleTextFile.txt as this

Some text with 2 white-spaces at the end  
Some other text with one white-space at the end 
No whit-espace at the end
Next line will be made of white-spaces

The EN

which will result in this output

Original text
Some text with 2 white-spaces at the end
Some other text with one white-space at the end
No whit-espace at the end
Next line will be made of white-spaces

The END

White-spaces as .
Some.text.with.2.white-spaces.at.the.end..
Some.other.text.with.one.white-space.at.the.end.
No.whit-espace.at.the.end
Next.line.will.be.made.of.white-spaces
.........
The.END

So you may want to check you program again where you may trim the strings yourself.