Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StreamWriter add an extra \r in the end of the line

I created a class with the responsibility to generate a text file where each line represents the information of an object of 'MyDataClass' class. Below is a simplification of my code:

public class Generator
{
    private readonly Stream _stream;
    private readonly StreamWriter _streamWriter;
    private readonly List<MyDataClass> _items;

    public Generator(Stream stream)
    {
        _stream = stream;
        _streamWriter = new StreamWriter(_stream, Encoding.GetEncoding("ISO-8859-1"));
    }

    public void Generate()
    {
        foreach (var item in _items)
        {
            var line = AnotherClass.GetLineFrom(item);

            _streamWriter.WriteLine(line);
        }

        _streamWriter.Flush();
        _stream.Position = 0;
    }
}

And I call this class like this:

using (var file = new FileStream("name", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    new Generator(file).Generate();
}

When I run the application on visual studio (I test with run (Ctrl+F5), debug (F5), with debug and release mode) all goes according to the plan. But I publish the application in a IIS server and now StreamWriter class put an extra \r before the end of the line.

Check it out the hexadecimal reading of both generated files:

Running in Visual Studio: http://www.jonataspiazzi.xpg.com.br/hex_vs.bmp

Running in IIS: http://www.jonataspiazzi.xpg.com.br/hex_iis.bmp

Some things I already checked:

Write the line variable (in var line = AnotherClass.GetLineFrom(item);) in a log to see if an extra '\r' is uncluded by the class AnotherClass.

Didn't result in nothing, the last char in line is a regular char like expected (in example above is a space).

Write another code to see if the problem is general for all IIS StreamWriter instances.

I tried this:

var ms = new MemoryStream();
var sw = new StreamWriter(ms, Encoding.GetEncoding("ISO-8859-1"));

sw.WriteLine("Test");
sw.WriteLine("Of");
sw.WriteLine("Lines");

sw.Flush();
ms.Position = 0;

In this case the code works well for both visual studio and IIS.

I'm in this for 3 days, I already try everything my brain can think. Did anyone have any clue for what I can try?

UPDATE

Get weirder! I try to replace the line _streamWriter.WriteLine(line); with:

_streamWriter.Write(linhaTexto + Environment.NewLine);

And even worse:

_streamWriter.Write(linhaTexto + "\r\n");

Both keep generating the extra \r character.

I try replace with this:

_streamWriter.Write(linhaTexto + "#\r\n#");

And get:

http://www.jonataspiazzi.xpg.com.br/hex_sharp.bmp

like image 843
Jonny Piazzi Avatar asked Apr 17 '15 17:04

Jonny Piazzi


1 Answers

According to MSDN, WriteLine

Writes data followed by a line terminator to the text string or stream.

your last line should be

 _streamWriter.Write(line);

Put it outside of your loop and change your loop so it doesn't manage the last line.

like image 129
Fjodr Avatar answered Sep 19 '22 14:09

Fjodr