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 ret开发者_Go百科urns 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.
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.
I tested your code.
alt text http://img830.imageshack.us/img830/5443/18414385.png
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.
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 ?
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
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.
If each body
string's first character was '\r', it would explain what you're seeing.
Have you checked whether body
is also ending with characters you don't want printed? This is the other potential problem source.
精彩评论