[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

53
zzyzzyxx•

Your problem is that std::string operates on char data, 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 say str.substr(1,1), it pulls out one char (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::wstring instead of std::string, which uses wchar_t data instead of char data. 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::string is 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 with std::string and think of everything as a sequence of bytes instead of a sequence of characters.

3
cottiOP•

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.

1
Rhomboid•

Characters always have some encoding, which is a way of saying how to map between characters and bytes. There are countless different encodings, and they all work differently. Some treat a character as a single byte, some treat a character as a small but fixed number of bytes ("wide characters"), some treat a character as a varying number of bytes ("multibyte characters").

Which encoding you are using depends on your locale setting. It's impossible to talk about specifics without knowing what encoding you are using. But in general, being able to properly work with character data means using data structures and APIs/functions that are appropriate for the encoding. If you have character data encoded in a variable width encoding, then you can't simply just index into it and print a character at a time as you're trying to do, because the "1 byte = 1 char" assumption does not hold. std::string assumes that, and so it's hopeless to use it in this situation, again assuming that you're dealing with a variable width encoding such as UTF-8 which is the most popular encoding.

It is also possible to convert between encodings. The encoding that you use inside your program might not be the same as the encoding you use to read input or the encoding you use to write output. For instance, you might want to read UTF-8 input and convert that to wide characters so that you can use std::wstring which is a typedef for basic_string<wchar_t>.

If you get nothing else out of this comment, make it this: You absolutely, positively cannot talk about working with characters or strings without specifying an encoding. Most of the time, an explicit encoding is not mentioned in which case ASCII (or some other single byte encoding like latin-1 or cp1252 -- the ambiguity of not specifying is always a huge problem!) is assumed. But as you've seen, ASCII is not a suitable encoding in the modern world, and so now we have things like UTF-8.

3