64,637
社区成员
发帖
与我相关
我的任务
分享
//VS2010 MFC,Unicode
void CBrowseFileDlg::OnBnClickedBrowse() //浏览文件按钮按下处理函数
{
// TODO: 在此添加控件通知处理程序代码
CString strPathName;
CString strFileName;
CString strText;
//浏览文件对话框
CFileDialog fileDlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("所有文件(*.*)|*.*||"),NULL,NULL,false);
if(fileDlg.DoModal() == IDOK)
{
FILE *pFile;
strPathName = fileDlg.GetPathName(); //获得文件路径
strFileName = fileDlg.GetFileName(); //获得文件名称
//CString转char*
const size_t strSize = (strFileName.GetLength() + 1) * 2;
char *pCh = new char[strSize];
size_t sz = 0;
wcstombs_s(&sz,pCh,strSize,strFileName,_TRUNCATE);
pFile = fopen(pCh,"rb");
fseek(pFile,0,SEEK_END);
int nFileSize = ftell(pFile); //获得文件大小(字节)
double dKB = nFileSize / 1024; //获得文件大小(千字节)
double dMB = dKB / 1024; //获得文件大小(兆字节)
//下面,调试的时候打开文件显示的字节、MB都正常,KB的两位小数总是.00,怎么回事啊
strText.Format(_T("文件名:%s\r\n文件大小:%d 字节(%.2f KB, %.2f MB)"),strFileName,nFileSize,dKB,dMB);
m_file.SetWindowTextW(strPathName); //将文件路径显示到m_file编辑框
m_detail.SetWindowTextW(strText); //将文件详细信息显示到m_detail编辑框
}
}
#include <stdio.h>
char *human(__int64 v) {
__int64 g;
int i,j;
char N[8]=" KMGTPE";
static char s[20];
g=512i64;
for (i=0;i<6;i++) {
if (v<g) break;//
g=g<<10;
}
j=(i-1)*10;
if (j<0) j=0;
if (j==0) {
if (i==0) {
sprintf(s,"%I64d B",v);
} else {//i==1
sprintf(s,"%.4lg KB",(double)v/1024.0);
}
} else {//j>0
v=v>>j;
sprintf(s,"%.4lg %cB",(double)v/1024.0,N[i]);
}
return s;
}
int main() {
__int64 d,t;
while (1) {
printf("Input a number(0..%I64d):",0x7FFFFFFFFFFFFFFFi64);
fflush(stdout);
rewind(stdin);
if (1==scanf("%I64i",&d)) {
if (d>=0i64) break;//
}
}
printf("%I64d is %s\n",d,human(d));
printf("\n");
d=0i64;t=512i64;
while (1) {
printf("0x%016I64x is %s\n",d,human(d));
d+=t>>2;
if (d>=t) {
d=t-1i64;
printf("0x%016I64x is %s\n",d,human(d));
d=t;
printf("0x%016I64x is %s\n",d,human(d));
if (d>=0x2000000000000000i64) {
d=0x7FFFFFFFFFFFFFFFi64;
printf("0x%016I64x is %s\n",d,human(d));
break;//
}
if (d<0x0800000000000000i64) {
d=d<<8;
t=t<<10;
} else {
t=d<<1;
d+=t>>2;
}
}
}
return 0;
}
//Input a number(0..9223372036854775807):0x33333333333
//3518437208883 is 3.199 TB
//
//0x0000000000000000 is 0 B
//0x0000000000000080 is 128 B
//0x0000000000000100 is 256 B
//0x0000000000000180 is 384 B
//0x00000000000001ff is 511 B
//0x0000000000000200 is 0.5 KB
//0x0000000000020000 is 128 KB
//0x0000000000040000 is 256 KB
//0x0000000000060000 is 384 KB
//0x000000000007ffff is 512 KB
//0x0000000000080000 is 0.5 MB
//0x0000000008000000 is 128 MB
//0x0000000010000000 is 256 MB
//0x0000000018000000 is 384 MB
//0x000000001fffffff is 512 MB
//0x0000000020000000 is 0.5 GB
//0x0000002000000000 is 128 GB
//0x0000004000000000 is 256 GB
//0x0000006000000000 is 384 GB
//0x0000007fffffffff is 512 GB
//0x0000008000000000 is 0.5 TB
//0x0000800000000000 is 128 TB
//0x0001000000000000 is 256 TB
//0x0001800000000000 is 384 TB
//0x0001ffffffffffff is 512 TB
//0x0002000000000000 is 0.5 PB
//0x0200000000000000 is 128 PB
//0x0400000000000000 is 256 PB
//0x0600000000000000 is 384 PB
//0x07ffffffffffffff is 512 PB
//0x0800000000000000 is 0.5 EB
//0x0c00000000000000 is 0.75 EB
//0x0fffffffffffffff is 0.999 EB
//0x1000000000000000 is 1 EB
//0x1800000000000000 is 1.5 EB
//0x1fffffffffffffff is 1.999 EB
//0x2000000000000000 is 2 EB
//0x7fffffffffffffff is 7.999 EB
double dKB = nFileSize / 1024.0; //获得文件大小(千字节)
double dMB = dKB / 1024; //获得文件大小(兆字节)