我需要确定文件的字节大小。
编码语言是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/