Solved

How to read long interger?

Posted on 2000-05-16
3
192 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
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

830 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