开发者

File output to Notepad .txt

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?


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.


Here is a 'recursive' solution for you:

  • select the box above that shows the correct output and copy it to the clipboard
  • open notepad
  • paste it into notepad (it'll look correct)
  • save the notepad file (select Unicode or UTF-8 encoding) and analyze it to find out what you need to write to get that output

I'm sure you can guess from the above that to get these characters you'll have to write a Unicode text file.


More sophisticated text editors (like Notepad++) let you choose the encoding. UTF-8 will show you the symbols you expect.


If you want to see these line drawing characters in the editor, you will need to select the Windows Terminal font.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜