// to compile: // c++ test_file_exists.cpp -lboost_filesystem -lboost_system #include #include #include #include #include #include #include using namespace std; bool stat_exists(const std::string& name) { struct stat buffer; return (stat (name.c_str(), &buffer) == 0); } bool stream_exists(const std::string &name) { std::ifstream infile(name.c_str()); return infile.good(); } int main(int argc, char *argv[]) { if (argc != 2) { cerr << "usage: " << argv[0] << " pathname" << endl; exit (EXIT_FAILURE); } string pathname = argv[1]; bool exist1 = boost::filesystem::exists(pathname); bool exist2 = stat_exists(pathname); bool exist3 = stream_exists(pathname); cout << "reply of boost::filesystem::exists: " << std::boolalpha << exist1 << endl; cout << "reply of stat_exists: " << std::boolalpha << exist2 << endl; cout << "reply of stream_exists: " << std::boolalpha << exist3 << endl; return EXIT_SUCCESS; }