C ++%f点代替逗号 [英] C++ %f dot instead of comma

查看:93
本文介绍了C ++%f点代替逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取 c ++ 中的文件,其中浮点数(%f)带有点而不是逗号.

I'm reading a file in c++ where floating point numbers (%f) have a dot instead of comma.

文件中的每一行都有3个这样的数字

Each line in the file has 3 numbers like that

1.000 -1.000 0.000

例如.

我的应用不想读取点号后的数字,所以如果我愿意

My app does not want to read the number past the dot, so If I do

fscanf(file, "%f %f %f\n", &x, &y, &z );

例如x设置为1,其他所有内容都保留为0,因为它只读取第一个点.

for example x is set to 1, everything else is left on 0 because it only reads to the first dot.

我已经读过某个地方,应该使用 setlocale 或类似的方法来更改语言环境,但没有成功.

I've already read somewhere that I should change the locale using setlocale or something similar but it didn't work out.

有人可以解释一下如何正确执行操作,或者是否有其他方法?

Could someone please explain how to do it properly or if there is another way?

我从标题中删除了"QT".首先是因为我认为QT可能有一些特殊的设置或针对语言环境的设置.奇怪的是,我的 ST3 默认C++ Single File构建系统已经将语言环境设置为使用点而不是逗号,但是我必须在QT中进行设置.每个编译器都有自己的默认语言环境设置吗?

I removed "QT" from the title. First it was there because I thought that maybe QT has some special settings or something for locales. It's weird that my ST3 default C++ Single File build system has the locale already set to use dots instead of commas, but I had to set it in QT. Does every compiler have their own default locale setting?

推荐答案

您可以添加该行

setlocale(LC_ALL, "C"); 

在调用fscanf之前.确保添加

#include <locale.h>

可以呼叫setlocale.

有关setlocale的更多信息,请访问 http://en.cppreference.com/w /c/locale/setlocale .

More on setlocale can be seen at http://en.cppreference.com/w/c/locale/setlocale.

这篇关于C ++%f点代替逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆