基于对Size of file using C++17的响应,我编写了以下程序。但是,当可执行文件运行时,出现分段错误。我正在运行High Sierra的iMac上使用g ++ 8.3.0。
// c17filesize.cpp
// Jul-02-2019
#include <cstring>
#include <filesystem>
using namespace std;
int main(int argc, char **argv)
{
char filename[100];
(argc > 1) ? strcpy(filename, argv[1]) : strcpy(filename, __FILE__);
auto size = filesystem::file_size(filename);
}
答案 0 :(得分:4)
在尝试读取文件大小std :: filesystem :: exists(filename)之前检查文件是否存在;
使用try和catch部分来捕获异常。
答案 1 :(得分:1)
使用std :: filesystem:exists时碰到了段错误。
使用GCC 8.3。必须与-lstdc ++ fs链接以解决该问题。
注意:GCC 9+解决了此问题(https://gcc.gnu.org/gcc-9/changes.html)
答案 2 :(得分:0)
使用gcc 9.1.0进行的编译顺利完成,没有任何麻烦。