【发布时间】:2012-11-21 09:21:36
【问题描述】:
我的一个项目的 C++ 源代码中有一个愚蠢的错误。我在这部分做源 I/O 操作。我有一个愚蠢的错误,我打印 fscanf 读取值。在这部分下面: 首先,我没有读取好的值,当我打印一个浮点值时,我得到一个带有逗号“,”而不是点“。”的十进制值。在整数部分和浮点部分之间。
FILE* file3;
file3=fopen("test.dat","r");
float test1;
fscanf(file3," %f ",&test1);
printf("here %f\n",test1);
float test3 = 1.2345;
printf("here %f\n",test3);
fclose(file3);
test.dat 文件包含“1.1234”,我执行:
here 1,000000
here 1,234500
所以,我做了一个用 g++ 编译的简单测试 C 程序:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* file3;
float test3;
file3=fopen("test.dat","r");
fscanf(file3,"%f",&test3);
printf("here %f\n",test3);
fclose(file3);
}
它给出了:
here 1.123400
这是我第一次遇到这样的错误。任何人都可以看到有什么问题吗?
【问题讨论】:
-
用g++编译的C程序是C++程序。
-
这取决于区域设置。如果您的程序必须表现出特定的行为(例如使用
.作为小数分隔符),则必须相应地设置语言环境。 -
@nos:不,这很明显。这是语言环境初始化的区别。