如何在不打开文件的情况下检查C ++中是否存在文件?

时间:2019-03-04 22:41:38

标签: c++

我想编写代码来检查我的桌面上是否存在.exe文件并返回相应的消息。但是,我想执行此检查而不必打开文件。

2 个答案:

答案 0 :(得分:1)

您可以使用boost文件系统

#include <boost/filesystem.hpp>
boost::filesystem::exists("path/to/myfile.exe");

或者如果您具有C ++ 17兼容的编译器:

#include <filesystem>
std::filesystem::exists("path/to/myfile.exe");

答案 1 :(得分:0)

在Linux上,最喜欢的文件搜索和大小调整工具是stat。我认为它运行良好,无法打开或读取文件。

函数“ int64_t fileExists(常量字符串和aPFN)”使用 stat 。 (请参阅“ int exec()”的for循环)。

#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

我桌面上的可能输出(已编辑以减少输出):

   > ./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