Writing a Unix Style Text File in C #

I am trying to write a text file with new Unix style characters with my C # program.

For some reason, the following code does not work:

TextWriter fileTW = ... fileTW.NewLine = "\n"; fileTW.WriteLine("Hello World!"); 

Also this:

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

In both cases, '\ n' is replaced by '\ r \ n', which I don't want! I tested this with a hex editor that shows every line ending in 0x0D0A.

Any ideas?

Thanks!

EDIT:

Sorry, false alarm!

Let me explain ...

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

Sorry if I wasted any time! Thanks!

+6
source share
2 answers

I can not reproduce this. Code example:

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

Further:

 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 

Pay attention to the size - 6 bytes - 5 for "Hello" and one for "\ n". Without setting the NewLine property, it is equal to 7 (two for "\ r \ n").

Can you find a similar short but complete program demonstrating the problem? How do you determine that your file contains "\ r \ n" afterwards?

+12
source

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

 [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 to modify one of them and update your answer using your script.

0
source

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


All Articles