CFile ...

Posted on 2002-05-20
Last Modified: 2006-11-17
Hi Experts,

This is what i'm tryting to...

CFile f;
CString fName = "C:\\Test\\test.txt";
int fsize;
char buff[20];
unsigned long mylong;

f.Open(fName, CFile::modeRead, NULL);
fsize = myFile.GetLength();
f.Read(buff, fsize);
mylong = atol(buff);

Note:  test.txt contains a long value of more than 10 digits (e.g. 2523223232)

The question is, why when i print out mylong the another file, it gave me a negative number?

This is how i printed out...

FILE *fp
fp = fopen(filename,"w+");
fprintf (fp, "Test value = %ld\n", mylong);

Please help. Thanks!


Question by:TommyN14
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
LVL 30

Accepted Solution

Axter earned 30 total points
ID: 7022610
You're using an unsigned long to print to a long symbol in the prinf statement.

Try the following:
fprintf (fp, "Test value = %ul\n", mylong);

Author Comment

ID: 7022634
Hi Axter,

I tried that and now i get an extra "1" at the end.  For example, from my above example, now i get
25232232321.  Do you have any idea?

LVL 30

Expert Comment

ID: 7022704
Try the following instead:
printf("Test value = %u\n", mylong);
Technology Partners: 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!

LVL 30

Expert Comment

ID: 7022707
fprintf (fp, "Test value = %u\n", mylong);

What you saw at the end was not a 1, but an 'l' (L).

I forgot to remove it.

Author Comment

ID: 7022737
Hi Axter,

Great!!!  But this is not work for a number MORE THAN 10 digits.  For example 25232232324564.

Maybe i have to use double instead of long?  and what is the % should i use for double or unsigned double?

Thanks for your quick responsed.


Expert Comment

ID: 7023159
The problem is that a unsigned long is 4 bytes long (under windows - might be different for other OS's), so that means it can only handle values up to 4294967296. You could go for unsigned double, which will give you 8 bytes i.e. values up to (roughly) 1.84467E+19. Then you would use something like:

fprintf (fp, "Test value = %-20.0f\n", mylong);

If you want more than that, you would need to go to custom multi-precision "variables" which can handle almost (depending on memory) any mount of digits. But that's another kettle of fish altogether...

Expert Comment

ID: 7023673
For a double you need %lf

Your not quite right about the above limits, you should look up the following help sections for your compiler:

Data Types
printf Format specifiers

But in general:
4 byte
-2,147,483,648 to 2,147,483,647

float (7 digit prec)
4 byte
1.18x10^-38 to 3.40x10^38

double (15 digit prec)
8 byte
2.23x10^-308 to 1.79x10^308

long double (18 digit prec)
10 byte
3.37x10^-4932 to 1.18x10^4932

Author Comment

ID: 7024320
Thanks experts.  I'd like to have pts split out to 2 experts (Axter 30 pts and mirtol 20 pts).  How do i do that?

Expert Comment

ID: 7024351
LoungeLizard has it close enough. An unsigned long can actually only hold numbers up to 4294967295, but that's only 1 off of what he said (Remember 0 is a number, and it's 4294967296 different values, so 4294967296 - 1).

We were talking about unsigned long, so we take the negatives and add them to the positives.

If you are going to be working with huge integers, I recommend making your own custom storage class for the large numbers. Switching to doubles or floats can lead to inaccuracies, especially with the larger or smaller numbers.

Try breaking up the number at every 9 digit interval. Since you have it in a string, that's not going to be too difficult. Once you have done this, parse each 9 digit section individually into its own variable, and remember which variable is the most significant. You could patch together many variables like this, maybe in an array.

I have written the following sample program to better explain my point:

void main()
    int NumOfVariablesInArray;
    long *DynLongArray;
    int SizeOfBuff;
    char buff[20] = "1234567890123";
    char *buffptr;
    int arrayPos;

    //Allocate memory
    SizeOfBuff = strlen(buff);  //Assuming buff is already read in
    NumOfVariablesInArray = SizeOfBuff/9+1;
    DynLongArray = (long *)malloc(NumOfVariablesInArray*sizeof(long));  //Could use calloc too
     printf("Out of memory. Since we didn't request much (did we?) this is really bad. Does this look normal:%s?", buff);

    //Start filling DynLongArray with the values
    buffptr = buff+SizeOfBuff;
    arrayPos = NumOfVariablesInArray;
     sscanf(buffptr, "%i", DynLongArray+arrayPos);
     *buffptr = 0;
    sscanf(buff, "%i", DynLongArray);

    //Now prove we have stored it properly
    printf("I have stored this number:\n");
    for(int i = 0; i<NumOfVariablesInArray; i++)

    //Free memory

Expert Comment

ID: 7024520
Per your request, I have split points for you.
Points for mirtol -> Please comment here for your share:
Moondancer - EE Moderator

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 135
Which Linux flavors will this run on? 6 103
c++, dynamic object by json 1 60
How do i run a c++ file? 15 52
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

730 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