I would like to write code that checks if a .exe file exists on my desktop and return a corresponding message. However I would like to do this check without having to open the file.
Asked
Active
Viewed 6,399 times
0
-
[`std::filesystem::exists`](https://en.cppreference.com/w/cpp/filesystem/exists)? Note: It requires compiler, that supports C++17. – Algirdas Preidžius Mar 04 '19 at 22:42
-
Since you mention EXE is this Windows? Possible duplicate of [How can we check if a file Exists or not using Win32 program?](https://stackoverflow.com/questions/3828835/how-can-we-check-if-a-file-exists-or-not-using-win32-program) see also https://stackoverflow.com/questions/2112252/how-do-i-check-whether-a-file-exists-in-c-for-a-windows-program – Dave S Mar 04 '19 at 22:54
-
2Possible duplicate of [What’s the best way to check if a file exists in C++? (cross platform)](https://stackoverflow.com/questions/268023/what-s-the-best-way-to-check-if-a-file-exists-in-c-cross-platform) – AlbertM Mar 04 '19 at 23:10
2 Answers
2
You can use boost filesystem
#include <boost/filesystem.hpp>
boost::filesystem::exists("path/to/myfile.exe");
or if you have a C++17 compatible compiler:
#include <filesystem>
std::filesystem::exists("path/to/myfile.exe");

AlbertM
- 1,246
- 14
- 25
0
On Linux, my favorite tool for file search and sizing is stat. I think it performs well, and does not open or read the file.
Function "int64_t fileExists (const string& aPFN)" uses stat. (see for-loop of "int exec()").
#include <chrono>
// 'compressed' chrono access --------------vvvvvvv
typedef std::chrono::high_resolution_clock HRClk_t; // std-chrono-hi-res-clk
typedef HRClk_t::time_point Time_t; // std-chrono-hi-res-clk-time-point
typedef std::chrono::nanoseconds NS_t; // std-chrono-nanoseconds
using namespace std::chrono_literals; // support suffixes like 100ms, 2s, 30us
using std::chrono::duration_cast;
// Note: compile with -std=c++17 for the using comma list
#include <iostream>
using std::cout, std::cerr, std::endl;
#include <iomanip>
using std::setw;
#include <string>
using std::string;
#include <cstring> // strerror() function
#include <sys/stat.h>
class T952_t // ctor and dtor compiler provided defaults
{
public:
int operator()(int argc, char* argv[]) {
return exec(argc, argv); } // functor entry
private: // methods
int exec(int argc, char* argv[])
{
Time_t start_ns = HRClk_t::now();
if (argc < 2) { cerr << "\n Requires one or more filenames.\n"; return(-1); }
for (int i=1; i<argc; ++i) {
cout << "\n pfn: " << setw(20) << argv[i] << " ";
int64_t fStat = fileExists(argv[i]);
if (fStat >= 0) cout << "found with " << fStat << " bytes";
else cerr << strerror(errno) << " (errno: " << errno << ")";
}
auto duration_ns = duration_cast<NS_t>(HRClk_t::now() - start_ns).count();
std::cout << endl << "\n T952_t::exec() duration "
<< duration_ns << " ns" << std::endl;
return 0;
}
// ---------------------------------------------------------------
// Linux, from <sys/stat.h>, using stat()
// returns file size when file exists (file size 0 can exist)
// else -1 with errno set
int64_t fileExists (const string& aPFN)
{
struct stat statBuff; // random values when file does not exist
// returns 0 when file does exist, and statBuff filled in
// else -1 and errno set
int sRslts = stat (aPFN.c_str(), &statBuff);
if(-1 == sRslts) { statBuff.st_size = -1; }
return (statBuff.st_size);
} // 1.1 us / 0.8 us
//
// struct stat {
// dev_t st_dev; /* ID of device containing file */
// ino_t st_ino; /* inode number */
// mode_t st_mode; /* protection */
// nlink_t st_nlink; /* number of hard links */
// uid_t st_uid; /* user ID of owner */
// gid_t st_gid; /* group ID of owner */
// dev_t st_rdev; /* device ID (if special file) */
// off_t st_size; /* total size, in bytes <<<<<<<<<<<<<<<<<<<<<<<<<< */
// blksize_t st_blksize; /* blocksize for file system I/O */
// blkcnt_t st_blocks; /* number of 512B blocks allocated <<<<<<<<<<<<<<< */
// time_t st_atime; /* time of last access */
// time_t st_mtime; /* time of last modification */
// time_t st_ctime; /* time of last status change */
// };
}; // class T952_t
int main(int argc, char* argv[]) { return T952_t()(argc, argv); } // call functor
Possible output from my desktop (edited to reduce) :
> ./dumy952 Makefile.i686 xyzzyy dumy9?? abcd dumy9*.cc pupp
pfn: xyzzyy No such file or directory (errno: 2)
pfn: dumy952 found with 164864 bytes
pfn: dumy953 found with 356576 bytes
pfn: dumy954 found with 150952 bytes
pfn: dumy955 found with 131464 bytes
... skip
pfn: abcd No such file or directory (errno: 2)
... skip
pfn: dumy952.cc found with 3257 bytes
pfn: dumy953.cc found with 3481 bytes
pfn: dumy954.cc found with 857 bytes
pfn: dumy955.cc found with 2172 bytes
... skip
pfn: pupp No such file or directory (errno: 2)
T952_t::exec() duration 855838 ns

2785528
- 5,438
- 2
- 18
- 20