标签:
1 #include <iostream> 2 #include <windows.h> 3 using namespace std; 4 5 6 /*根据文件大小进行格式化 7 *@[in ] llBytes 文件的长度(B) 8 *@[out] pszSize 缓冲区 9 *@[in ] clen 缓冲区长度 10 */ 11 12 BOOL FileSizeToStringA(LONGLONG llBytes, char* pszSize, DWORD cLen) 13 { 14 double bytes = (double)llBytes; 15 DWORD cIter = 0; 16 char* pszUnits[] = { ("B"), ("KB"), ("MB"), ("GB"), ("TB") }; 17 DWORD cUnits = sizeof(pszUnits) / sizeof(pszUnits[0]); 18 19 // move from bytes to KB, to MB, to GB and so on diving by 1024 20 while(bytes >= 1024 && cIter < (cUnits-1)) 21 { 22 bytes /= 1024; 23 cIter++; 24 } 25 _snprintf_s(pszSize, cLen, _TRUNCATE,("%.2f %s"), bytes, pszUnits[cIter]); 26 return TRUE; 27 } 28 29 int main() 30 { 31 char szText[256]; 32 FileSizeToStringA(23156,szText,256); 33 34 cout << "szText:" << szText << endl; 35 36 getchar(); 37 return 0; 38 }
标签:
原文地址:http://www.cnblogs.com/chechen/p/4378508.html