Solved

CString & atol problem

Posted on 2004-04-24
3
833 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 100 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

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 87
dog bark java program 15 81
Hibernate methods 2 58
fix34  challenge 9 96
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 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

16 Experts available now in Live!

Get 1:1 Help Now