C # remove extra carriage returns from stream

Im reading a file as a stream: Stream fin = File.OpenRead(FilePath); Is there a way in which I can find and remove all the carriage returns from this file stream?

EDIT: The goal is to remove single carriage returns \rand leave the carriage returned, which with a new line "\r\n"intact.

Example file:

The key backed up in this file is:\r
\r\n
pub   2048R/65079BB4 2011-08-01\r
\r\n
      Key fingerprint = 2342334234\r
\r\n
uid                  test\r
\r\n
sub   2048R/0B917D1C 2011-08-01\r

And the result should look like this:

The key backed up in this file is:
\r\n
pub   2048R/65079BB4 2011-08-01
\r\n
      Key fingerprint = 2342334234
\r\n
uid                  test
\r\n
sub   2048R/0B917D1C 2011-08-01

EDIT2: The final solution that works is as follows:

    static private Stream RemoveExtraCarriageReturns(Stream streamIn)
    {
        StreamReader reader = new StreamReader(streamIn);
        string key = reader.ReadToEnd();
        string final = key.Replace("\r\r", "\r");
        byte[] array = Encoding.ASCII.GetBytes(final);
        MemoryStream stream = new MemoryStream(array);
        return stream;
    }

I use Streamuse StreamReaderto read it in a line, and then remove the extra carriage return and write it back to Stream. Does my code look normal or should I do something different?

+5
3

"r":

string text = File.ReadAllText(FilePath);

text = text.Replace("\r\r", "\r");

File.WriteAllText(FilePath + ".modified", text);
+2

:

string final = string.Join("", File.ReadLines(path));

? , .

+2

Replace line break in C # line

The above answer. You can read a line in a file and then remove line breaks.

0
source

All Articles