Check if file exists

This commit is contained in:
Matt McWilliams 2023-02-03 16:15:44 -05:00
parent 40a97baad8
commit 840f6743f9
2 changed files with 15 additions and 4 deletions

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
mkdir build mkdir -p build
cd build cd build
cmake .. cmake ..
make -j make -j

View File

@ -1,9 +1,15 @@
#include <iostream> #include <iostream>
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
#include <sys/stat.h>
using namespace std; using namespace std;
using namespace cv; using namespace cv;
inline bool file_exists (const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
string input; string input;
@ -11,14 +17,19 @@ int main(int argc, char** argv)
if (argc == 2) { if (argc == 2) {
input = argv[1]; input = argv[1];
} else if (argc < 2) { } else if (argc < 2) {
cerr << "Please provide one image" << endl; cerr << "Please provide one image." << endl;
return -1; return -1;
} else if (argc > 2) { } else if (argc > 2) {
cerr << "Please provide only one image" << endl; cerr << "Please provide only one image." << endl;
return -2; return -2;
} }
cout << "Using image " << input << endl; if (!file_exists(input)) {
cerr << "File " << input << " does not exist." << endl;
return -3;
}
cout << "Using image " << input << "." << endl;
return 0; return 0;
} }