Why does Qt change behaviour of sscanf()?

Viewed 1910

I have noticed, Qt (4.8) changes the behaviour of sscanf(). Without Qt sscanf() works as usual, but with, it takes only localized strings.

Here's a minimized example:


Without Qt (plain C++)

int main(int argc, char *argv[])
{
    float f;
    sscanf("0.83", "%f", &f);

    std::cout << f << "\t-->\t" << typeid("0.83").name() << std::endl;

    return 0;
}

Output:

0.83    -->     A5_c

(given string is a 5x char-array, result correct)


With Qt

int main(int argc, char *argv[])
{
    /*
     * This breaks sscanf() for the whole (!) project
     * and linked libraries too!
     */
    QApplication(argc, argv);

    float f;
    sscanf("0.83", "%f", &f);

    std::cout << f << "\t-->\t" << typeid("0.83").name() << std::endl;

    return 0;
}

Output:

0       -->     A5_c

(Given string still a 5x char-array, but result is wrong)


While 0.83 fails, using 0,83 (my locale format) works fine with Qt - but fails without Qt (default behaviour). As shown by typeid(), there's no QString used - only plain old C(++) char-arrays. Btw., same happens to std::string.

Beside this, using a std::stringstream keeps working as normal:

std::stringstream ss;
ss << "0.83"; // But the value into the stream
ss >> f;      // Get a float out of it

Result:

0.83

And here comes the question: Why are char-array strings and sscanf() calls affected by Qt? I understand why QString's are localized, but breakingsscanf() (and potentially other stdio.h functions) sounds evil to me.

Background: I linked a (non-Qt) library, containing a sscanf() somewhere deep in the code, to a Qt project. Result: Some code failed in this project, while it worked everywhere else … (took some time to find the cause …)

3 Answers
Related