Output error on directory

This commit is contained in:
rexy712 2019-07-22 13:29:53 -07:00
parent 2a733a0224
commit 49a1f0456f

View File

@ -34,6 +34,8 @@
#include <chrono> //std::chrono::*
#include <cstdlib> //srand
#include <string> //char_traits
#include <sys/types.h> //struct stat
#include <sys/stat.h> //struct stat
constexpr const char version_string[] = "roflcat version 1.0.4";
@ -135,11 +137,23 @@ void nonprinting_notation(int in, char* dest){
}
}
bool is_directory(const char* file){
struct stat st;
if(stat(file, &st) != 0){
return true;
}
return S_ISDIR(st.st_mode);
}
FILE* open_file(const char* file, const char* mode){
if(is_directory(file)){
fflush(stdout);
fprintf(stderr, "Unable to open file \"%s\": Is a directory \n", file);
return nullptr;
}
FILE* fp = fopen(file, mode);
if(!fp){
fflush(stdout);
fprintf(stderr, "Unable to open file %s\n", file);
fprintf(stderr, "Unable to open file for reading: \"%s\"\n", file);
}
return fp;
}