Can I make StreamReader "believe" that EOL is now "###" and not "\ n"?

I have a file and I need to read it after the line. Each line ends with "###", and not with a regular line terminator (\ n).

Is there a way to change the streamReader, so when I use ReadLine (), it will read until it sees "###"? If not, is there another way to do this, or should I implement a new class for this purpose?

+3
source share
5 answers

No, you cannot do this with StreamReader. The StreamReader class is hard-coded for character recognition \rand \n, and it is not configurable. You can see this with .NET Reflector:

char ch = this.charBuffer[charPos];
switch (ch)
{
    case '\r':
    case '\n':
        string str;
        if (builder != null)
        // ...

, , ###. , - ReadLine , , .

+2

[] Moles framework, Environment.NewLine

      MEnvironemnt.NewLineGet = () => "###"; 

, streamreader [/]

, streamreader ...

0

StreamReader.ReadLine.

, , , , TextReader, , MS (http://referencesource.microsoft.com/netframework.aspx)

0

I can come up with two ways to do this. The first is to write an extension method in StreamReader that does what you want and use it instead. The second is to define a new encoding object that will translate ### into \ n (and possibly translate \ n to something else).

0
source

One option (depending on performance) is to make it simple read()and look for "###" in the input stream to do whatever you want with it.

0
source

Source: https://habr.com/ru/post/1756938/


All Articles