【问题标题】:Finding the last created FILE in the directory, C++在目录中查找最后创建的文件,C++
【发布时间】:2023-04-09 12:01:01
【问题描述】:

即使我在互联网上搜索过,也没有像我这样的问题。我的问题是,我想获取目录中创建的最后一个文件的名称。我的系统将在此代码的目录中创建来自我的相机的 /.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

在这段代码中,我尝试为 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 对象的引用。

【问题讨论】:

  • 什么是实时码?是功能吗?
  • 我的脚本名称,我现在编辑了以防进一步混淆
  • 正如编译器建议的那样:它是 FILETIME*(指针),而不是 FILETIME(值)
  • 我编辑了,但就像我说的我的问题是在 if() 状态下比较文件时间,换句话说,有没有办法将文件时间与初始值进行比较?
  • 你认为我应该将文件时间转换为四字之后与简单的欢迎符号“

标签: c++ file directory


【解决方案1】:

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

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;
}

【讨论】:

  • 兄弟,我能说什么,我爱你,(亲吻)
  • 但我有一个问题,你为什么使用 // name = finder.GetFileName().GetString() 我看不到有什么大的区别吗?
  • GetFilename() 返回一个CString。但是您的函数需要返回一个 std::string (名称属于该类型)。为了将 CString 转换为 std::string,我调用 GetString(),它是 CString 的一种方法。请注意,GetString() 本身返回一个 const char 指针,然后可以将其分配给 name。
猜你喜欢
  • 1970-01-01
  • 2019-11-11
  • 1970-01-01
  • 2012-10-06
  • 2021-01-12
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-05-29
相关资源
最近更新 更多