c++ - 在 C/C++ 中获取文件大小的可移植方法

标签 c++ c portability filesize platform-independent

我需要确定文件的字节大小。

编码语言是C++,代码应该适用于Linux、windows和任何其他操作系统。这意味着使用标准 C 或 C++ 函数/类。

这个微不足道的需求显然没有微不足道的解决方案。

最佳答案

使用 std 的流,您可以使用:

std::ifstream ifile(....);
ifile.seekg(0, std::ios_base::end);//seek to end
//now get current position as length of file
ifile.tellg();

如果你处理只写文件(std::ofstream),那么方法是另外一些:

ofile.seekp(0, std::ios_base::end);
ofile.tellp();

关于c++ - 在 C/C++ 中获取文件大小的可移植方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2424138/

相关文章:

c++ - boost::thread 程序在抛出 std::exception 时崩溃

c++ - 在 C++ 中的窗口上接收挂起的拖放操作的通知

c - 如何从 srand(time(0)) 知道 secret 数字

eclipse - 带插件的可移植 Eclipse Juno

c - 不应该在 API 中使用枚举吗?

c++ - 使此 C++ 代码通用

c++ - 将 boost (asio) 错误消息翻译成自然语言

c - 如果请求的内存空间很大,Malloc 是否只使用堆?

c++ - 以下代码以意想不到的方式获取输入

c - C 语言的可移植 BIT 宏