空格字符/流编写器问题 C#

发布于 2024-11-30 02:30:16 字数 424 浏览 1 评论 0原文

我试图制作一个固定宽度的文件,但我发现当我使用空格字符时,我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2024-12-07 02:30:16

请忽略您之前在这里读到的内容。我获取了你的程序的输出,它在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文