空格字符/流编写器问题 C#
我试图制作一个固定宽度的文件,但我发现当我使用空格字符时,我的txt文件中有一个奇怪的标志。如果我将“50”数字更改为“15”或“1000”之类的数字,则效果很好。
这是我的代码:
string fileName = Path.GetTempPath() + "temp.txt";
FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream);
writer.Write("1".PadRight(50, ' '));
writer.Write("0".PadRight(756, ' '));
writer.Close();
出了什么问题?
I tried to make a fixed width file, but I found that when I use the space character I have a strange signs in my txt file. If I change the "50" number to something like "15" or "1000", it works fine.
This is my code:
string fileName = Path.GetTempPath() + "temp.txt";
FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream);
writer.Write("1".PadRight(50, ' '));
writer.Write("0".PadRight(756, ' '));
writer.Close();
What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请忽略您之前在这里读到的内容。我获取了你的程序的输出,它在 Microsoft 记事本中确实显示得很奇怪,并在其他几个编辑器中打开了它。在所有这些中看起来都很好。我确信这是一些编码错误,然后我破解了 XVI32 以查看二进制文件。
我从文件末尾删除了一个空格,然后重新保存文件,它显示得很好。我把它放回去,问题又出现了。我将第一个十六进制字符从 32 更改为小于 30 的任何值,效果很好。
因此,这对于 Microsoft 记事本来说似乎是一个非常奇怪的错误。
Disregard what you read here previously. I took the output of your program, which does shows up strange in Microsoft Notepad, and opened it in a few other editors. It appears fine in all of them. Convinced it was some encoding error, I then cracked open XVI32 to look at the binary.
I removed a single space from the end of the file and I re-save the file and it shows up fine. I put it back in and the problem returned. I change the first hex character from 32 to anything less than 30 and it works fine.
So this seems like a very bizarre error with Microsoft Notepad.