
郭老师为您分享以下优质知识
获取二进制文件长度的方法取决于所使用的编程语言和平台。以下是常见编程语言中的实现方式:
一、C 中获取文件长度
使用 `System.IO.File` 类的 `Length` 属性:
```csharp
string filePath = "example.bin";
long fileSize = System.IO.File.Length(filePath);
Console.WriteLine($"文件大小: {fileSize} 字节");
```
对于大文件,建议使用 `FileStream` 分块读取以避免内存不足:
```csharp
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
long fileSize = fs.Length;
// 可以按需分块读取
}
```
二、C++ 中获取文件长度
1. 使用 C++17 及以上版本的 `std::filesystem`:
```cpp
include
include
int main() {
std::filesystem::path filePath = "example.bin";
std::uintmax_t fileSize = std::filesystem::file_size(filePath);
std::cout