Solved

CString & atol problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sum67 challenge 35 93
fizzArray  challenge 1 57
MaxSpan challenge 9 79
wordappend challenge 8 143
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 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

15 Experts available now in Live!

Get 1:1 Help Now