gpt4 book ai didi

c - 使用 fscanf 确定您正在读取 int 还是 double

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:36 25 4
gpt4 key购买 nike

我有一个文本文件,我想用 C 来读取它。它包含以下格式的行:%d %d (%d or %lf)。 3 行的示例:

1 0 44
2 0 84.13
3 1 15.07

但最后一个数字可以是 intdouble,任意。无论数据类型如何,我如何从文本文件中读取?

最佳答案

因为 float 可以容纳一个整数,但反之则不行。只需像 float 一样读取数据并使用类似

的方法检查它是否为整数
if(ceilf(f) == f)
{
i=(int)f;
}
//Here i is an integer and f is the float you read using %f

要查看有关如何检查 float 是否为整数的更多方法,请参阅

Checking if float is an integer

关于c - 使用 fscanf 确定您正在读取 int 还是 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30335262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com