Reputation: 53
I have a text file that I want to read using C. It contains lines in this format: %d %d (%d or %lf)
. An example of 3 lines:
1 0 44
2 0 84.13
3 1 15.07
But the last number can be either int
or double
, arbitrary. How can I read from the text file regardless of the data type?
Upvotes: 1
Views: 581
Reputation: 536
Since float can hold an integer but not vice versa. Just read the data like a float and check if it is an integer using something like
if(ceilf(f) == f)
{
i=(int)f;
}
//Here i is an integer and f is the float you read using %f
To see more methods on how to check if a float is int see
Checking if float is an integer
Upvotes: 1