Solved

CString & atol problem

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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