为什么Qt改变sscanf()的行为? [英] Why does Qt change behaviour of sscanf()?
问题描述
我注意到,Qt(4.8)改变了 sscanf()
的行为。如果没有Qt sscanf()
就像往常一样工作,但是,它只需要本地化的字符串。
这是一个最小化的例子:
没有Qt(普通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;
返回0;
}
输出:
0.83 - > A5_c
(给定的字符串是5x char
-array,result correct)
随着Qt
<$ p $ (!)项目$ b中的sscanf()会中断($)$!
$ $ b *和链接库也是!
* /
QApplication(argc,argv);
float f;
sscanf(0.83,%f,& f);
std :: cout<< f< \ t - > \ t<< typeid(0.83)。name()<<的std :: ENDL;
返回0;
}
输出:
0 - > A5_c
(给定字符串仍然是5x char
$ b
$ b 虽然 0.83
失败,使用 0,83
(我的区域设置格式)与Qt一起工作正常 - 但没有Qt(默认行为)失败。如 typeid()
所示,没有使用 QString
- 只有普通的旧C(++)char-arrays。顺便说一句,同样发生在 std :: string
。
除此之外,使用 std :: stringstream
保持正常工作:
std :: stringstream ss;
ss<< 0.83; //但流中的值
ss>> F; //获取一个浮点数
结果:
0.83
问题在于:为什么char-array字符串和 sscanf()
调用受Qt影响?我明白为什么 QString
是本地化的,但是打破 sscanf()
(可能还有其他
$ b
背景:我链接了一个(非Qt)库,在代码深处包含一个 sscanf()
到Qt项目。结果:一些代码在这个项目中失败,而它在其他地方工作...(花了一些时间来找到原因...)
一般而言,如果您在标准库中获得与流或I / O操作有关的函数,则很可能会受到语言环境设置的影响。
这对于 sscanf
是正确的,在你的情况下,Qt覆盖了你通常得到的默认的 C语言环境
当使用默认的C / C ++配置时。
您应该使用
setlocale LC_NUMERIC,C)
在初始化Qt环境后,在本例中为 QApplication
。
https ://qt-project.org/doc/qt-5/qcoreapplication.html#locale-settings
可以让您的设置恢复您的预期。
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 …)
In general terms if you get a function, in the standard library, that has something to do with streams or I/O operations, chances are that it's gonna be affected by your locale settings.
This is true for sscanf
and in your case Qt is overriding the default C locale
that you usually get when using the default C/C++ configuration.
you should use
setlocale(LC_NUMERIC,"C")
right after initializing your Qt environment, in this case after QApplication
.
https://qt-project.org/doc/qt-5/qcoreapplication.html#locale-settings
to set things back to what you expect .
这篇关于为什么Qt改变sscanf()的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!