【发布时间】:2013-08-08 15:40:02
【问题描述】:
如何在继续我的程序之前检查文件是否存在于我的目录中?我已经阅读了尝试使用各种方法打开文件的答案,但我的问题是大多数时候,我正在检查的文件将损坏并且无法打开。这发生在我的程序的错误检查部分,只有在前面的代码中发生错误时才会触发。我想检查文件是否存在,如果是这样,请要求删除它,否则只需打印出一些消息。
我该怎么办?
(只是删除并接受错误会起作用,但我这样做是为了学习,所以我想正确地做......)
编辑:
我已经下载了 Boost 来使用文件系统库并编译它,看起来没有错误,但是当我尝试编译我的程序时,我得到了这个响应:
g++ program.cpp -I <path to>/boost_1_54_0 -o output
Undefined symbols for architecture x86_64:
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::exists(boost::filesystem::path const&)in cc1XX8rD.o
"boost::system::system_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in cc1XX8rD.o
"boost::system::generic_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in cc1XX8rD.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
我在程序中使用 boost 的唯一地方是:
boost::filesystem::path my_file(s4);
if (boost::filesystem::exists(my_file)){ ...
【问题讨论】:
-
你能不能简单地(尝试)删除文件并容忍删除尝试触发的任何错误,以防它不存在?
标签: c++ macos file error-handling delete-file