tags:

views:

141

answers:

7

I have a very simple console application that creates a text file. Below is a recap of the code:

StreamWriter writer = File.CreateText("c:\\temp.txt");
foreach (blah...)
{
  writer.Write(body.ToString() + "\n");
  writer.Flush();
}
writer.Close();

The client is claiming there are carriage returns at the end of each line. Where are these carriage returns coming from?

Update: After opening in VS binary editor and Notepad++, there were no occurrences of 0d 0a. I'm going to go back to the client.

+1  A: 

The "\n" at the end of each write call

EDIT: I know this is a new line, not a carriage return but I bet any money the client is getting confused between the two and it's actually this that is causing the problem

w69rdy
That's a line feed, not a carriage return.
BoltClock
`\n` is a line feed. `\r` is carriage return.
ChrisF
@BoltClock, @ChrisF - I know, but unless there is a carraige return at the end of the body, this'll be what the client is complaining about
w69rdy
the "\n" will create a new line in the text file and this will be what the client is complaining about.
skyfoot
How do you write a new line without the "\n"?
Ryan Strauss
You can't, I would go back to the client, explain the situation and find out why it is causing a problem before you try to fix it
w69rdy
+1  A: 

In your code you put a line feed (\n).

Your customer is talking about a carriage return (\r). Maybe your customer is taking a line feed per a carriage return ?

Pierre 303
A: 

Does the client distinguish between a CR and LF? Is the flush() necessary? Are you overloading the buffer if you don't flush?

Unless you have a massive amount of text you might find more use out of creating a StringBuilder to format the text exactly as you want it with \n, \r, \t or whatever and then pumping that directly into a StreamWriter.

Joel Etherton
A: 

If each body string's first character was '\r', it would explain what you're seeing.

David Gladfelter
+3  A: 

Open the file in the Visual Studio binary editor (File.Open.File, click down-arrow on Open button, choose Open With... and pick Binary Editor), and look for 0D bytes. If none are present, then either:

  • your client can't tell the the difference between a line feed and a carriage return,

  • your transmission method is modifying the file en-route. Is there any FTP binary/ascii mismatch going on?

If there are 0D bytes, then they are present in your body variable.

Neil Moss
Or the client is opening the file with a tool/function that automatically replaces \n with \r\n. Many Windows text editors do so.
0xA3
@Neil Moss: This is good. I did find the occurrence of 0d 0a between lines when opened in the VS binary editor but not with Notepad++. So it must exist in the body variable? I'll update my original post w/ the code.
Ryan Strauss
FYI, the client didn't know what they were talking about since the file did not have any carriage returns.
Ryan Strauss
A: 

Have you checked whether body is also ending with characters you don't want printed? This is the other potential problem source.

Dinah
+3  A: 

I tested your code.

alt text

The code you posted does not have any carriage returns (0D) only new lines (0A). Something else is creating the carriage returns or the client does not know what a carriage return really is.

0A0D