Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file exists without opening it

How can I check if a file exists in my directory before continuing with my program? I have read answers that try opening the file using various methods but my issue is that most of the time, the file I am checking for will be corrupt and cant be opened. This happens in the error checking part of my program and will only be triggered when an error in the preceding code has occurred. I want to check if the file exists, if so then ask to delete it, otherwise just print out some message.

How can I go about this?

(Just deleting and accepting the errors would work, but I'm doing this to learn, so I want to do it properly...)

Edit:

I have downloaded Boost to use the filesystem library and have compiled it, seemingly with no errors, but when I try to compile my program, I get this response:

g++ program.cpp -I <path to>/boost_1_54_0 -o output

Undefined symbols for architecture x86_64:
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
  boost::filesystem::exists(boost::filesystem::path const&)in cc1XX8rD.o
"boost::system::system_category()", referenced from:
  __static_initialization_and_destruction_0(int, int)in cc1XX8rD.o
"boost::system::generic_category()", referenced from:
  __static_initialization_and_destruction_0(int, int)in cc1XX8rD.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

The only places I use boost in the program are:

boost::filesystem::path my_file(s4);
if (boost::filesystem::exists(my_file)){ ...
like image 354
Sam Avatar asked Aug 07 '13 10:08

Sam


People also ask

How do you check file is exist or not?

The exists() function is a part of the File class in Java. This function determines whether the is a file or directory denoted by the abstract filename exists or not. The function returns true if the abstract file path exists or else returns false.

How do I check whether a file exists without exceptions?

To check whether a Path object exists independently of whether is it a file or directory, use my_path. exists() . my_path.

How do I check if a file exists in C++?

Use ifile. open() is mainly used to check if a file exists in the specific directory or not. In the filing, a stream refers to an abstract that signifies a method where input as well as output processes are executed. “ifile. open()” takes one argument that is the name of the file.

How do I check if a file exists in HTML?

The FileExists method returns a Boolean value that indicates whether a specified file exists. It returns True if the file exists and False if not.


1 Answers

Use stat() or access():

#include <unistd.h>

int res = access(path, R_OK);
if (res < 0) {
    if (errno == ENOENT) {
         // file does not exist
    } else if (errno == EACCES) {
         // file exists but is not readable
    } else {
         // FAIL
    }
}
like image 113
David Xu Avatar answered Oct 05 '22 12:10

David Xu