文件输出到记事本.txt
我无法用语言表达清楚,所以我只能陈述事实。
情况: 我的 C++ 程序将扩展 ascii 字符输出到文本文件。
问题: 当我用记事本打开文本文件时,它无法正确显示这些字符。 (我使用的是Windows XP)
结论: 如果我不得不猜测,我会说记事本使用错误的编码保存我的文件。有没有办法修复它,以便我的程序将正确的输出保存到文本文件中?
代码片段:
char box[] = {
201, 205, 187,
186, 32, 186,
200, 205, 188
};
当我 outputFile << box[0]
,我的预期结果是“╔”。相反,记事本显示“É”。
预期输出:
╔═════╗
║1. ║
║ ║
║ ║
╚═════╝
记事本输出:
ÉÍÍÍÍÍ»
º1. º
º º
º º
ÈÍÍÍÍͼ
编辑:好的。我现在明白我的错误了。记事本使用 ANSI 编码。为什么当我运行程序并将其输出到屏幕时,它显示为“╔”?我正在使用 Dev-C++ 编写程序 - 这是否意味着我正在使用过时的编码? C++ 有没有办法改变我使用的字符编码?
I can't word this well, so I'll just state the facts instead.
Situation:
My C++ program outputs extended ascii characters to a text file.
Problem:
When I open up my text file with Notepad, it displays those characters incorrectly. (I am using Windows XP)
Conclusion:
If I had to guess, I would say that Notepad is saving my file using the wrong encoding. Is there a way to fix it so my program saves the correct output to the text file?
Snippet of code:
char box[] = {
201, 205, 187,
186, 32, 186,
200, 205, 188
};
When I outputFile << box[0]
, my expected result is "╔". Instead, Notepad displays an "É".
Expected Output:
╔═════╗
║1. ║
║ ║
║ ║
╚═════╝
Notepad Output:
ÉÍÍÍÍÍ»
º1. º
º º
º º
ÈÍÍÍÍͼ
EDIT: Ok. I understand now my mistake. Notepad uses ANSI encoding. Why is it that when I run my program and cout it to the screen, it displays as "╔"? I am using Dev-C++ to write my programs - does that mean I am using an out-of-date encoding? And is there any way in C++ to change the encoding of characters that I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在“扩展 ASCII”中使用的方框图字符已过时。默认情况下,记事本可能使用 Latin-1 (ISO-8859-1)编码,代表不同的字符集。
Your use of the box drawing characters in "extended ASCII" is obsolete. By default, Notepad may be using Latin-1 (ISO-8859-1) encoding, which represents a different set of characters.
这是一个适合您的“递归”解决方案:
我确信您可以从上面猜测到要获得这些字符您必须编写一个 Unicode 文本文件。
Here is a 'recursive' solution for you:
I'm sure you can guess from the above that to get these characters you'll have to write a Unicode text file.
更复杂的文本编辑器(例如 Notepad++)允许您选择编码。 UTF-8 将显示您期望的符号。
More sophisticated text editors (like Notepad++) let you choose the encoding. UTF-8 will show you the symbols you expect.
如果您想在编辑器中看到这些线条画字符,则需要选择 Windows 终端 字体。
If you want to see these line drawing characters in the editor, you will need to select the Windows Terminal font.