Solved

How to read long interger?

Posted on 2000-05-16
3
204 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:chenwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 2813625
23900000000 does not fit in a 32 bit integer. Unless your system supports 36 or 64 bit integers, you may want to scan  %lf instead.
0
 
LVL 4

Expert Comment

by:wylliker
ID: 2814328
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.

0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2815274
looks like these values are floats, not integers... use the %lf as ozo suggested...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question