[英]StreamWriter is writing carriage returns?
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
. 更新:在VS二进制编辑器和Notepad ++中打开后,没有出现
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. 在Visual Studio二进制编辑器中打开文件(File.Open.File,在“打开”按钮上单击向下箭头,选择“打开方式...”,然后选择“二进制编辑器”),然后查找
0D
字节。 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?
FTP二进制/ ASCII不匹配持续吗?
If there are 0D
bytes, then they are present in your body
variable. 如果有
0D
字节,那么他们出现在你的body
变化。
I tested your code. 我测试了您的代码。
alt text http://img830.imageshack.us/img830/5443/18414385.png 替代文字http://img830.imageshack.us/img830/5443/18414385.png
The code you posted does not have any carriage returns ( 0D
) only new lines ( 0A
). 您发布的代码没有换行符(
0D
),只有换行符( 0A
)。 Something else is creating the carriage returns or the client does not know what a carriage return really is. 其他原因正在创建回车,或者客户不知道什么是回车。
The "\\n"
at the end of each write
call 每个
write
调用结束时的"\\n"
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 编辑:我知道这是一个新行,而不是回车,但我敢打赌,这两个客户之间的客户会感到困惑,这实际上是导致问题的原因
In your code you put a line feed (\\n). 在您的代码中放入换行符(\\ n)。
Your customer is talking about a carriage return (\\r). 您的客户正在谈论回车(\\ r)。 Maybe your customer is taking a line feed per a carriage return ?
也许您的客户每次回车都要换行?
Does the client distinguish between a CR and LF? 客户是否区分CR和LF? Is the flush() necessary?
是否需要flush()? 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. 除非您有大量的文本,否则您可能会发现创建StringBuilder来使用\\ n,\\ r,\\ t或其他内容来完全格式化所需的文本格式,然后将其直接泵送到StreamWriter中,将有更多用处。
如果每个body
字符串的第一个字符为'\\ r',它将解释您所看到的内容。
Have you checked whether body
is also ending with characters you don't want printed? 您是否检查过
body
是否也以您不想打印的字符结尾? This is the other potential problem source. 这是另一个潜在的问题来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.