Search code examples
c++eclipsemacosutf-8special-characters

Writting special characters to console in C++ on OSX


I have written a program in C++ to query music-related meta-data. However, when writing results to console I realized that some of the artists have special characters in their name, UTF-8 to be specific. So far I have learned to display special characters by their character codes like so:

cout << "\u0394" << "\n";

The code above should display "Δ" according to http://www.utf8-chartable.de/ but instead it displays "Œî" and I believe this has to do with the encoding of the console.

I did some research on the issue and came across a sample program that should straiten out my issue by instead I get the following errors that I cannot seem to get around.

#include <io.h>
#include <fcntl.h>
#include <iostream>

int main()
{
    _setmode (_fileno(stdout), _O_U8TEXT);
    wcout << L"AaĄąfl" << endl;

    return 0;
}

../main.cpp:1:16: warning: io.h: No such file or directory
../main.cpp: In function 'int main()':
../main.cpp:7: error: '_fileno' was not declared in this scope
../main.cpp:7: error: '_O_U8TEXT' was not declared in this scope
../main.cpp:7: error: '_setmode' was not declared in this scope
../main.cpp:8: error: 'wcout' was not declared in this scope
../main.cpp:8: error: 'endl' was not declared in this scope
make: *** [main.o] Error 1

I am aware that there is loads of information on this out there but I have done enough research, in my opinion, to warrant posting here. One of the reasons that I believe I cannot solve this issue is because none of the information I have read on this topic works for MacOSX. I am also using Eclipse Kepler.

Any information on how to get my sample program to work or display to the console correctly would be greatly appreciated.

Thank you


Solution

  • Thanks to @sbugert...

    How to change the encoding of the console in Eclipse Kepler:
    Run > Run Configurations
    Navigate to your current project
    Enter the 'common' tab
    See 'encoding' section