I want to check if a character is printable, but the input comes from the user so I cannot be sure it is ASCII. I tried to use the wide functions, but it does not work.
See this little program:
#include <iostream>
#include <clocale>
#include <string>
int main() {
std::setlocale(LC_ALL, "");
std::wstring str{};
std::wcin >> str;
std::wcout << "\n";
for (auto& c : str) {
if (std::isprint(c))
std::wcout << c << " is printable\n";
else
std::wcout << c << " is NOT printable\n";
}
std::wcout "\n" << str << std::endl;
}
It compiles fine, but the result works only for the second and third character!
% ./test
かたな
か is NOT printable
た is printable
な is printable
かたな
What am I missing? Is there a way to check if a character is printable that works from standard input whatever locale is there? Or something more generic depending on the encoding?
Aucun commentaire:
Enregistrer un commentaire