Link to home
Start Free TrialLog in
Avatar of chenwei
chenwei

asked on

How to read long interger?

I am writting a program and I use sscanf to read data from a file:

sscanf(line, "%ld %lf\n", &a, &b);

The fiel contains data like following:

6000000.000  0.734581
23900000000.000  0.674805  
24000000000.000   0.606476  
24100000000.000   0.521484  

As I read in the first line, it's ok. But from the second line, I got problem. The a is not the value it is but -1869635475. It's an error. It seems the integer is too big. How can I solve the problem?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wylliker
wylliker

The limit is  4,294,967,295 for unsigned long values and 2,147,483,647 for signed long values.

You would have to read in your line differently and deal with creating your own math routines if you have to deal with these numbers as non-floats or non-doubles (i.e. integers) in calculations.

You could, for example take the right-most 9 characters and keep track of that as Billionths and the remaining left-most characters as Billions.
Your numbers would become a struct ...

struct _UglyNumber
{
   unsigned long billionths;
   unsigned long billions;
   unsigned short sign;
}

You would probably do better to use a float or double.

looks like these values are floats, not integers... use the %lf as ozo suggested...