使用 fscanf 确定您正在读取 int 还是 double
Find out if you're reading int or double using fscanf
我有一个文本文件,我想用 C 来读取它。它包含以下格式的行:%d %d (%d or %lf)
。 3行的例子:
1 0 44
2 0 84.13
3 1 15.07
但最后一个数字可以是 int
或 double
,任意。无论数据类型如何,我如何从文本文件中读取?
因为浮点数可以容纳一个整数,但反之则不行。只需像浮点数一样读取数据并使用
之类的方法检查它是否为整数
if(ceilf(f) == f)
{
i=(int)f;
}
//Here i is an integer and f is the float you read using %f
要查看有关如何检查浮点数是否为整数的更多方法,请参阅
Checking if float is an integer
我有一个文本文件,我想用 C 来读取它。它包含以下格式的行:%d %d (%d or %lf)
。 3行的例子:
1 0 44
2 0 84.13
3 1 15.07
但最后一个数字可以是 int
或 double
,任意。无论数据类型如何,我如何从文本文件中读取?
因为浮点数可以容纳一个整数,但反之则不行。只需像浮点数一样读取数据并使用
之类的方法检查它是否为整数if(ceilf(f) == f)
{
i=(int)f;
}
//Here i is an integer and f is the float you read using %f
要查看有关如何检查浮点数是否为整数的更多方法,请参阅
Checking if float is an integer