Solved

How to read long interger?

Posted on 2000-05-16
3
168 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
Comment Utility
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
Comment Utility
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
Comment Utility
looks like these values are floats, not integers... use the %lf as ozo suggested...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now