From 840f6743f9e3db1178f9498eb947a525becd046d Mon Sep 17 00:00:00 2001 From: mattmcw Date: Fri, 3 Feb 2023 16:15:44 -0500 Subject: [PATCH] Check if file exists --- compile.sh | 2 +- example.cpp | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/compile.sh b/compile.sh index 01e3b07..1b05d44 100644 --- a/compile.sh +++ b/compile.sh @@ -1,6 +1,6 @@ #!/bin/bash -mkdir build +mkdir -p build cd build cmake .. make -j diff --git a/example.cpp b/example.cpp index 9bc1dee..fadd57c 100644 --- a/example.cpp +++ b/example.cpp @@ -1,8 +1,14 @@ #include #include +#include using namespace std; 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) { @@ -11,14 +17,19 @@ int main(int argc, char** argv) if (argc == 2) { input = argv[1]; } else if (argc < 2) { - cerr << "Please provide one image" << endl; + cerr << "Please provide one image." << endl; return -1; } else if (argc > 2) { - cerr << "Please provide only one image" << endl; + cerr << "Please provide only one image." << endl; 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; } \ No newline at end of file