c++ - Check if a file exists without opening it -
how can check if file exists in directory before continuing program? have read answers try opening file using various methods issue of time, file checking corrupt , cant opened. happens in error checking part of program , triggered when error in preceding code has occurred. want check if file exists, if ask delete it, otherwise print out message.
how can go this?
(just deleting , accepting errors work, i'm doing learn, want properly...)
edit:
i have downloaded boost use filesystem library , have compiled it, seemingly no errors, when try compile program, response:
g++ program.cpp -i <path to>/boost_1_54_0 -o output undefined symbols 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 architecture x86_64 collect2: ld returned 1 exit status
the places use boost in program are:
boost::filesystem::path my_file(s4); if (boost::filesystem::exists(my_file)){ ...
use stat()
or access()
:
#include <unistd.h> int res = access(path, r_ok); if (res < 0) { if (errno == enoent) { // file not exist } else if (errno == eacces]) { // file exists not readable } else { // fail } }
Comments
Post a Comment