Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CString & atol problem

Posted on 2004-04-24
3
Medium Priority
?
847 Views
Last Modified: 2013-11-20
Hi I have a program that tells me the free hard-drive space in CSting (Ex. "25,976,307,712") and I'm basically taking that number, removing the commas ("25976307712"), and do a converison from string to long (atol).  Then I take the number from the 'atol' conversion and compare it against anonther predefined datatype 'long' number, to see if my free space is less than, let's say 250 MB.   But, when I do the 'atol' conversion on the number ("25976307712") it looks like I'm getting back a number of 206405632, thus it looks like my free space is less than 250mb, when in reality I have 24gigs of free space........ I'm suspecting that the 'atol' function is messing me up, any suggestions please?   I have included a copy of my code below: Thanks

//-------------------------------------------------------------------------------------------------------------
                long baseLine = 262144000;                  //250 MB in Bytes
      Sysinfo si_object;
      CString m_info=si_object.TNTGetInfo();  //GET SYSTEM INFO
      CString test = m_info.Mid(793,14);         //GET FREE SPACE INFO
                                                                           //"25,976,307,712" bytes
      char buffer[100000];
      

      MessageBox(test,"BEFORE remove commas",NULL);   //"25,976,307,712"
      test.Replace(",","");
      MessageBox(test,"AFTER removed commas",NULL);  //"25976307712"
   
       long  x = atol(test);    //TRYING TO CONVERT STRING TO LONG
      
      ltoa(x,buffer,10);        //WANTED TO SEE WHAT THE RESULT OF 'atol' was
      MessageBox(buffer,"BUFFER",NULL);  //RETURNS 206405632

      if(x < baseLine)    //206405632 < 262144000 (should be 25976307712 < 262144000 )
            MessageBox("DRIVE SPACE LOW","RESULT",NULL);
//-------------------------------------------------------------------------------------------------------------
0
Comment
Question by:arunykand
3 Comments
 

Expert Comment

by:ronzul
ID: 10912033
Hello

I would suggest that the problem is related to the maximum value of a long. A long is a 32-bit signed value, so the maximum positive value is somewhat over 2 billion. Way short of your 250 billion.

If you don't require accuracy down to the last byte, consider checking the number of digits in the string. If there are more than nine, chop off the last three digits before the atol and have a result in kilobytes. If there are more than 12, chop off the last 6 and have a result in megabytes.

Cheers
Ronny
0
 
LVL 4

Expert Comment

by:r2far
ID: 10912402
As an addition to ronzul's response... if you need the precision try using an __int64 or LARGE_INTEGER... but watch it cause math with these objects is sometimes different (LARGE_INTEGER's do not support math unless they are converted into __int64's)


__int64 = signed with max val under +/- 9223372036854775808.... if that isn't enough precision you better make your own integer class to handle more than 64 bits.
0
 
LVL 5

Accepted Solution

by:
Nass89 earned 400 total points
ID: 10937486
Hi,
 To solve your problem correct your codes as follows:

__int64 baseline;
 __int64   x = _atoi64(test);


_i64toa(x,buffer,10)
MessageBox(buffer,"BUFFER",NULL);  //RETURNS exactly

if(x<baseline)
                       ........

Good Luck!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

879 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