查找目录中最后创建的FILE,C++

2024-03-31

尽管我在网上搜索过,但没有任何像我这样的问题。我的问题是,我想获取目录中最后创建的文件的名称。我的系统将在该代码的目录中创建来自我的相机的 /.png 文件,我希望我的代码采用最后创建的文件。我想用这段代码来做,

string processName()
{
   // FILETIME Date = { 0, 0 };
   // FILETIME curDate;
   long long int curDate = 0x0000000000000000;
   long long int date    = 0x0000000000000000;
   //CONST FILETIME date={0,0};
   //CONST FILETIME curDate={0,0};
   string name;
   CFileFind finder;
   FILETIME* ft; 

   BOOL bWorking = finder.FindFile("*.png");
   while (bWorking)
   {

      bWorking = finder.FindNextFile();

      date = finder.GetCreationTime(ft) ;

      //ftd.dwLowDateTime  = (DWORD) (date & 0xFFFFFFFF );
      //ftd.dwHighDateTime = (DWORD) (date >> 32 );


      if ( CompareFileTime(date, curDate))
      {
          curDate = date;
          name = (LPCTSTR) finder.GetFileName();
      }



   }
   return name;
}

我没有使用额外的库,而是使用了以下库,可以在链接中看到:

https://msdn.microsoft.com/en-US/library/67y3z33c(v=vs.80).aspx https://msdn.microsoft.com/en-US/library/67y3z33c(v=vs.80).aspx

在此代码中,我尝试为 64 位 FILETIME 变量赋予初始值,并将它们与此 while 循环进行比较。但是我收到以下错误。

1   IntelliSense: argument of type "long long" is incompatible with parameter of type "const FILETIME *"        44  25  
2   IntelliSense: argument of type "long long" is incompatible with parameter of type "const FILETIME *"        44  31  

getCreationTime 方法采用一个参数作为

参数: pTimeStamp:指向包含文件创建时间的 FILETIME 结构的指针。

refTime:对 CTime 对象的引用。


我想我修复了你的代码。必须改变一些类型等:

string processName()
{
    FILETIME bestDate = { 0, 0 };
    FILETIME curDate;
    string name;
    CFileFind finder;

    finder.FindFile("*.png");
    while (finder.FindNextFile())
    {
        finder.GetCreationTime(&curDate);

        if (CompareFileTime(&curDate, &bestDate) > 0)
        {
            bestDate = curDate;
            name = finder.GetFileName().GetString();
        }
    }
    return name;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

查找目录中最后创建的FILE,C++ 的相关文章

随机推荐