Solved

CString & atol problem

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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