【发布时间】:2011-03-03 19:39:34
【问题描述】:
【问题讨论】:
标签: c# c++ visual-c++
【问题讨论】:
标签: c# c++ visual-c++
“计算”是什么意思?
您只需询问文件系统,“那个文件有多大?”它会告诉您长度,不涉及计算。
你的问题到底是什么?
如何在 C# 中做到这一点?
这里的代码会给你一个文件的长度,还有其他方法:
long length = new FileInfo(@"c:\temp\test.exe").Length;
【讨论】:
你可以在 C++ 中这样做:
直接取自cplusplus.com
// Obtaining file size
#include <iostream>
#include <fstream>
using namespace std;
int main () {
long begin,end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}
【讨论】:
如果你只想要文件的大小,你可以在 C# 中这样做:
long size = new System.IO.FileInfo("<path to file>").Length;
【讨论】:
另一个符合您要求的变体——使用 Windows 文件管理 API 的 Visual C++:
LARGE_INTEGER liFileSize;
HANDLE hFile;
// open/create file
GetFileSizeEx(hFile, &liFileSize);
// close handle
【讨论】:
使用统计数据。
struct stat buf;
fstat(fd, &buf);
int size = buf.st_size;
【讨论】: