Writing Unix style text file in C#
Asked Answered
A

2

10

I'm trying to write a text file with Unix-style newlines with my C# program.

For some reason the following code doesn't work:

TextWriter fileTW = ...

fileTW.NewLine = "\n";

fileTW.WriteLine("Hello World!");

Neither does this:

TextWriter fileTW = ...

fileTW.Write("Hello World! + \n");

In both cases the '\n' is being replaced with '\r\n', which I don't want! I've been verifying this with a hex editor, which shows each line ending in 0x0D0A.

Any ideas?

Thanks!

EDIT:

Sorry everyone, false alarm!

Allow me to explain...

My TextWriter was writing to a MemoryStream, which was then being added to a tar archive using SharpZLib. It turns out that by extracting the text file using WinZIP, it was replacing every instance of \n with \r\n. If I copy the same tar archive to my Ubuntu machine and extract there, only the \n is there. Weird!

Sorry if I wasted anyone's time! Thanks!

Arcature answered 20/10, 2011 at 20:20 Comment(4)
any reason you are using a TextWriter ? You can use a FileStram for example...Hallowed
https://mcmap.net/q/487681/-net-quot-default-line-terminator-quotScissors
I'm using the TextWriter to write to MemoryStream first.Arcature
It seems as if the /r is being added later on when I try to write my MemoryStream to file, and not by the TextWriter. Let me do a little digging and I'll get back to everyone! Thanks everyone for their time!Arcature
I
18

I'm unable to reproduce this. Sample code:

using System;
using System.IO;

class Test
{
    static void Main()
    {
        using (TextWriter fileTW = new StreamWriter("test.txt"))
        {
            fileTW.NewLine = "\n";            
            fileTW.WriteLine("Hello");
        }
    }
}

Afterwards:

c:\users\jon\Test>dir test.txt
 Volume in drive C has no label.
 Volume Serial Number is 4062-9385

 Directory of c:\users\jon\Test

20/10/2011  21:24                 6 test.txt
               1 File(s)              6 bytes

Note the size - 6 bytes - that's 5 for "Hello" and one for the "\n". Without setting the NewLine property, it's 7 (two for "\r\n").

Can you come up with a similar short but complete program demonstrating the problem? How are you determining that your file contains "\r\n" afterwards?

Indention answered 20/10, 2011 at 20:25 Comment(2)
Thanks Jon! Your code actually works as expected on my end too. It seems as if the /r is being added later on when I try to write my MemoryStream to file, and not by the TextWriter. Let me do a little digging and I'll get back to everyone!Arcature
@dan6470: I very much doubt that MemoryStream is doing anything like that. Can you tell us more about what you're doing, and how you're checking for \r\n afterwards?Indention
J
1

I'm in the same boat as Jon Skeet. Here's my tests against a MemoryStream that confirm it does use what you give it as the NewLine value.

[Test]
public void NewLineIsUnixStyle()
{
    using (var text = new MemoryStream())
    using (TextWriter writer = new StreamWriter(text))
    {
        writer.NewLine = "\n";

        writer.WriteLine("SO");
        writer.Flush();

        text.Position = 0;
        var buffer = new byte[10];
        var b3 = buffer[3];
        Assert.AreEqual(3, text.Read(buffer, 0, 10));
        Assert.AreEqual('S', (char)buffer[0]);
        Assert.AreEqual('O', (char)buffer[1]);
        Assert.AreEqual('\n', (char)buffer[2]);
        Assert.AreEqual(b3, buffer[3]);
    }
}

[Test]
public void NewLineIsSomeTextValue()
{
    using (var text = new MemoryStream())
    using (TextWriter writer = new StreamWriter(text))
    {
        writer.NewLine = "YIPPEE!";

        writer.WriteLine("SO");
        writer.Flush();

        text.Position = 0;
        var buffer = new byte[10];
        Assert.AreEqual(9, text.Read(buffer, 0, 10));
        Assert.AreEqual('S', (char)buffer[0]);
        Assert.AreEqual('O', (char)buffer[1]);
        Assert.AreEqual('Y', (char)buffer[2]);
        Assert.AreEqual('I', (char)buffer[3]);
        Assert.AreEqual('P', (char)buffer[4]);
        Assert.AreEqual('P', (char)buffer[5]);
        Assert.AreEqual('E', (char)buffer[6]);
        Assert.AreEqual('E', (char)buffer[7]);
        Assert.AreEqual('!', (char)buffer[8]);
        Assert.AreEqual(0, buffer[9]);
    }
}

Feel free modify one of these and update your answer with your scenario.

Josefinejoseito answered 20/10, 2011 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.