Solved

CFile ...

Posted on 2002-05-20
10
442 Views
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!

TommyN14

0
Comment
Question by:TommyN14
10 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 30 total points
Comment Utility
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);
0
 
LVL 1

Author Comment

by:TommyN14
Comment Utility
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?

TommyN14
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Ooops!!!
Try the following instead:
printf("Test value = %u\n", mylong);
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Example:
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.
0
 
LVL 1

Author Comment

by:TommyN14
Comment Utility
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.

TommyN14
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 2

Expert Comment

by:LoungeLizard
Comment Utility
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...
0
 
LVL 2

Expert Comment

by:mirtol
Comment Utility
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:
long
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
0
 
LVL 1

Author Comment

by:TommyN14
Comment Utility
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?
0
 
LVL 3

Expert Comment

by:Crius
Comment Utility
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
    if(!DynLongArray)
    {
     printf("Out of memory. Since we didn't request much (did we?) this is really bad. Does this look normal:%s?", buff);
     return;
    }

    //Start filling DynLongArray with the values
    buffptr = buff+SizeOfBuff;
    arrayPos = NumOfVariablesInArray;
    while(buffptr-9>buff)
    {
     buffptr-=9;
     arrayPos--;
     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++)
     printf("%i",DynLongArray[i]);

    //Free memory
    free(DynLongArray);
}
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
Per your request, I have split points for you.
Points for mirtol -> Please comment here for your share:
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20303133
Moondancer - EE Moderator
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 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

10 Experts available now in Live!

Get 1:1 Help Now