[C/C++] Handling special characters from argv
Hi there. I'm making some simple stuff to, for example, colorize an input, and maybe later make it ready for use in pipes in Linux. It already works for general ASCII input - both a C and a C++ version.
The problem is, I'm not a native English speaker, so characters with different punctuations are going to happen. And when I just print the argv without messing with it, it prints fine just as I wrote, but if I treat them as individual characters, an interrogation point appears instead.
Seemed like a locale stuff. I never dealt with this before, and tried setlocale(ALL,"") to enforce environment locale in the program, but it didn't work.
First I tried with pure C, with simple char[] meddling. Then I tried the newer wchar_t. Then I tried the code adapted to C++, using their string implementation, and trying to trick the program, basically saying "instead of printing a char... Print this string of 1 character."
Nothing worked. Can anyone shed a light on this? What's the secret to dealing with the argv as... "more than just chars", since the program DOES receive the string correctly?
This is a simple example of the C++ version of what I can't do (no protection or anything, just try something like $ ./program mérito . It will print "mérito" correctly, but fail to print the "é" alone.
#include <iostream>
#include <string>
using namespace std;
int main (int argc, char ** argv)
{
string str = string(argv[1]);
cout << str << endl << str.substr(1,1) << endl;
return 0;
}
Posted Nov 14, 2012, 8:31 AM UTC
Your problem is that
std::stringoperates onchardata, that is, exactly one byte at a time. This is fine for ASCII text, but not for most other characters like what you need. So when you saystr.substr(1,1), it pulls out onechar(again, just one byte) instead of one "character" because that character takes multiple bytes to represent. How that character is represented depends on the encoding. Read this, which explains the basics of character encodings.The reason you see your input appropriately when you input and print the entire string is because your terminal is performing the encoding and decoding of all the bytes together. When you select only one of those bytes, that byte represents a different character, or perhaps no valid character at all. From your test program, try printing a substring of 2 or 3 or 4 bytes. Eventually you will see the 'é' you expect; the number depends on the encoding your terminal is using.
To get this working how you like, you might be able to use
std::wstringinstead ofstd::string, which useswchar_tdata instead ofchardata. Read this for some insight on that. More likely, however, you will need a library to properly handle Unicode. For C and C++ there is ICU. Here is an SO link that has a link to ICU as well as some general guidance on proper use.Edit: A
std::stringis suitable for storing encoded data, but not for operating on it (since it works on bytes instead of code points), and that's where the libraries come in. So depending on what you need to do, it may be sufficient to simply stick withstd::stringand think of everything as a sequence of bytes instead of a sequence of characters.I actually did read that encoding article before, it's a good insight. What kept me beating my head was that ambiguous transformation between them.
About ICU, thanks a lot for the tip. I'll try to use it. What I am doing is stuff like adding a specific bash color code to each character, and printing them individually.