Essentially, your program writes a
sequence of bytes to the terminal, or into the file.
The terminal – or the text-editor that you are using to view the file – then
interprets those bytes as text. But, the outcome totally depends on what
character encoding (Latin-1, UTF-8, etc. pp.) the terminal – or the text-editor that you are using to view the file –
assumes when it interprets the given bytes as text characters!
For example: In "Latin-1" encoding (Windows-1252), the character
°
is encoded as a single byte with the value
0xB0
. But, in Code Page 437 (OEM), that very same characters is encoded as the byte value
0xF8
. And, in UTF-8 encoding, that very same character even is encoded as the byte
sequence 0xC2
+
0xB0
😏
https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Code_page_layout
https://en.wikipedia.org/wiki/Code_page_437#Character_set
https://en.wikipedia.org/wiki/UTF-8#Codepage_layout
_________
To make a long story short, be sure you write the text into the file in the
desired character encoding. Then, when you view the file in a text-editor, make sure the
matching character encoding is configured there.
https://i.imgur.com/IjRCgxO.png
Be aware, without additional meta-data, it is
not possible to know character encoding of a "plaint" text file.
std::string
is kind of "agnostic" to character encoding; it just stores
char
's (bytes).
Note: It can help to look at the "raw" bytes in your file with a
hex viewer in order to avoid confusion ;-)
https://mh-nexus.de/de/hxd/