chm*_*ike 8 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();
Run Code Online (Sandbox Code Playgroud)
如果你处理只写文件(std :: ofstream),那么方法是另一种方法:
ofile.seekp(0, std::ios_base::end);
ofile.tellp();
Run Code Online (Sandbox Code Playgroud)
您可以使用stat系统调用:
#ifdef WIN32
_stat64()
#else
stat64()
Run Code Online (Sandbox Code Playgroud)