Friends are like snow, they disappear when you pee on them.

Dieser Inhalt wird in Deutschland nicht angezeigt. Weitere Informationen

So I wrote a simple wavefront parser using POSIX regular expressions. It suddenly stopped working, while I was trying to port the whole thing to Qt. The parser itself is not very beautiful, but it did its job well so far, so why change it?

Well, I found out the problem lies in the following line:

sscanf(mytokens[1+i], "%lf", coords+i);

The variable mytokens[1+i] contains a string that contains a float, say, -0.697203. coords is an array of float values. However, suddenly, it always contained zero. I added the following line to debug this issue:

fprintf(stderr, "%s %lf\n", mytokens[1+i], coords+i);

The output consisted of lines of the form

-0.212040 0,000000
1.092960 0,000000
-0.679962 0,000000
-0.110031 0,000000
1.123250 0,000000

I noticed that the second numbers all have a comma as separator - which is common in Germany. And my locale is set to de_DE.UTF-8. And sscanf seems to also parse according to this locale.

Running my application with LANG=C worked again.

Update:

Maybe I should have given the solution to the problem. Well, the locale is set by Qt apparently. I could set it using setlocale(3), but then I would risk clashes with other parts of Qt.

Instead, as I use C++ anyway, I used a std::stringstream, and its method imbue. A quick and dirty replacement for sscanf looked like

static void parse_double (const string& dbl, double& ret) {
    locale l("C");
    stringstream ss;
    ss.imbue(l);
    ss << dbl;
    ss >> ret;
}