LONG not big enough
1 Attachment(s)
I am trying to get a harddrives free space. I can do it just fine, but I can't store the size correctly. LONG is not big enough; 4GB max.
LONG freeBytes=64016611328; (just to show an example size) (60GB) double or float is what I need, correct? The size could be over 1TB. But now I can't display it with sprintf() sprintf(buffer,"Free Space: %f",freeBytes); just prints the %f when display buffer. Can't the SAS/C (OS3.9) sprintf() handle floats and doubles? There will never be a decimal point. Is there some other method I need to use? In the image below, Size number blocks * 512 = 64016611328 |
you cannot format an integer type with %f..
maybe sas supports 'long long' ? |
This is a stupid aproach. Nobody wants the read the number of bytes of a harddrive if its size is measured in 10s of gigabytes.
Even megabytes are too much, but nevertheless you can use this to show megabytes: Code:
long blksize = InfoData->id_BytesPerBlock; |
Looks like you're just going to have to write a base 2^32 (or base 65536 or base 256) to base 10 conversion routine and construct the output string digit by digit.
Nearly trivial. |
@Thomas: As always, thanks for the pointer. But, no, I wasn't going to display the total bytes to the user. I first tested it on a small 2.5GB partition, then a floppy. It worked. Then tried the large partition, numbers all messed up. I wanted to see what was going on so I tried to display it in a requester. Then it dawned on me it was too large of a number. Sorry for trying to figure it out in a more visual manner.
|
Quote:
|
If you ever go nuts about your code,
#define DEBUG and put some lines inside ifdef DEBUG printff(format, var,..) endif removing with undef DEBUG or comment it out. Debug output will be supessed then. ?? Did you try to print your long var as this: sprintf(buffer,"Free Space: %ld",freeBytes); On some older compilers the printf family may fail to output formatted long(or anything bigger than int). In that rare case you need to convert them with ltoa() if available or do it with your own little conversion routine. |
Quote:
Quote:
Usually the printf in the clib only supports integer. It has to be replaced by the float-enabled printf frome the math library. |
You could store the size in kilobytes. This would extend your range from 4GB to 4TB.
|
Using flags MATH=STANDARD and PRECISION=MIXED. I was linking LIB:scm.lib. Without it I was getting link errors. Also tried with LIB:scmieee.lib. Same result.
I always use %ld in my sprintf() calls. With that it comes out a negative value (with ULONG). That is what got me started on this. I don't really need to "see" the total bytes, it was just a "WTF is going on?" moment. With Thomas' code I got started. I was just hoping to use double or float to get a deicmal like "59.5 GB". Maybe with the basic math functions I can still do it. Thanks for the help, guys. |
You can use something like this:
Code:
char *nice_size (char *buffer,unsigned long blksize,unsigned long numblks) |
I wrote something pretty much just like that, but my math was off. So switched to yours.
Just for the sake of it, I added this for terabytes: Code:
if (NumBlocks>=bpg) |
Two notes:
1. my routine works with binary megabytes (sometimes also called mebibytes). So your check for terabytes (tebibytes) should by if (size >= 102400). If you want to work with decimal megabytes, you should replace all occurences of 1024 by 1000 and 512 by 500. 2. nobody yet has proven whether advanced format harddrives (i.e. with 4096 byte blocks) will work on AmigaOS 3.x. Therefore you probably won't ever see a drive bigger than 2TB. So the check for terabytes might be overkill. |
1 Attachment(s)
I've installed SAS/C yesterday. Oh my god, that reminds me of a horrible experience when buying an introduction into asm that came with seka on floppy disc those days. SAS editor is hardcoded to key codes? No support for foreign layouts?
Having a quick peek in limits.h shows that there is no support for >32bit. Long and Int are same size. If you really need large numbers/adresses consider using a more modern compiler like vbcc or the like. You can always ask your compiler what's the max size of each type. E.g.: Code:
#include <stdio.h> Edit: when compiling with vbcc uncomment define and use -c99 as option. |
Quote:
But in most cases, like the one above, you don't need such a suffix, as the type is clear to the compiler. When assigning a long long variable the constants default to long long as well. So it works here, because the 'L' is ignored. |
Quote:
http://d3na4zxidw1hr4.cloudfront.net...30x328_q85.jpg |
I haven't had time to do a full test, but I noticed that when doing RAM, it is one level higher (GB instead of MB). I think it is because the block size is 1024. Any idea on that?
|
Code:
if (NumBlocks>=bpg) Code:
size = size / 1024; |
Not sure what you mean by that.
Either way, after some tweaking and additions to handle RAM in a different way (AvailMem()), it is all working good. |
Quote:
Quote:
Code:
int main (int argc,char **argv) Result: Code:
3> nice_size 512 2147483647 |
All times are GMT +2. The time now is 03:36. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.