I need to output some data that may be UTF-8 multi-byte and I need to keep them formatted using setw()
.
When the characters are multi-byte sequences, aligement is lost and setw()
doesn't work correctly.
//#include <stdio.h>
#include <locale>
#include <iostream>
//#include <fstream>
#include <iomanip>
//#include <sstream>
int main(int argc, char **argv)
{
std::locale l=std::locale("en_US.utf8");
std::locale::global(l);
std::cout.imbue(l);
std::cout<<std::endl;
std::cout<<std::setw(40)<<std::right<<"hi “my” friend"<<std::endl;
std::cout<<std::setw(40)<<std::right<<"hi -my- friend"<<std::endl;
return 0;
}
The output is:
hi “my” friend
hi -my- friend
What am I missing ?
I must point out that the characters “
and ”
are not the normal "
but instead two others, which in UTF-8 are expressed by three bytes each.