1 | // to compile:
|
---|
2 | // c++ test_file_exists.cpp -lboost_filesystem -lboost_system
|
---|
3 |
|
---|
4 | #include <cstdlib>
|
---|
5 | #include <string>
|
---|
6 | #include <iostream>
|
---|
7 | #include <iomanip>
|
---|
8 | #include <boost/filesystem.hpp>
|
---|
9 | #include <sys/stat.h>
|
---|
10 | #include <fstream>
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | bool stat_exists(const std::string& name) {
|
---|
15 | struct stat buffer;
|
---|
16 | return (stat (name.c_str(), &buffer) == 0);
|
---|
17 | }
|
---|
18 |
|
---|
19 | bool stream_exists(const std::string &name)
|
---|
20 | {
|
---|
21 | std::ifstream infile(name.c_str());
|
---|
22 | return infile.good();
|
---|
23 | }
|
---|
24 |
|
---|
25 | int main(int argc, char *argv[]) {
|
---|
26 | if (argc != 2) {
|
---|
27 | cerr << "usage: " << argv[0] << " pathname" << endl;
|
---|
28 | exit (EXIT_FAILURE);
|
---|
29 | }
|
---|
30 | string pathname = argv[1];
|
---|
31 | bool exist1 = boost::filesystem::exists(pathname);
|
---|
32 | bool exist2 = stat_exists(pathname);
|
---|
33 | bool exist3 = stream_exists(pathname);
|
---|
34 | cout << "reply of boost::filesystem::exists: " << std::boolalpha << exist1 << endl;
|
---|
35 | cout << "reply of stat_exists: " << std::boolalpha << exist2 << endl;
|
---|
36 | cout << "reply of stream_exists: " << std::boolalpha << exist3 << endl;
|
---|
37 | return EXIT_SUCCESS;
|
---|
38 | }
|
---|