【问题标题】:How to get File Time in C++如何在 C++ 中获取文件时间
【发布时间】:2011-07-07 21:31:03
【问题描述】:

我在获取文件时间创建时遇到问题。有人知道如何获取文件时间吗??

我有一个获取某个目录的文件名的代码我的问题是我不知道如何将文件时间放在目录中的每个文件上...

这是我的代码:

#include <vector>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <iostream> 
#include <conio.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <time.h>
#include <exception>
#include <WinBase.h>

        using namespace std;
int ifException(string directory) throw()
    {
        DWORD returnvalue;
        returnvalue = GetFileAttributes(directory.c_str());
            if(returnvalue == ((DWORD)-1))
            {
                return 0;
            }
            else
            {   
                return 1;
            }
}



    vector <string> GetPath(string path)
    {



            char directory[9999];
            vector <string> filelist;
            string buffer;
            strcpy_s(directory,path.c_str());
            BOOL checker;




                try
                    {
                        ifException(directory);

                    }catch(int i)
                    {   
                        if (i==0)
                        {
                            _getch();
                            exit(1);
                        }
                    }
                buffer = findFileData.cFileName;
                filelist.push_back(buffer);
                checker = FindNextFile(hFind, &findFileData);
                while(checker)
                {
                    if(checker == 0)
                    {
                        filelist.resize(filelist.size());
                        return filelist;
                    }
                    checker = FindNextFile(hFind, &findFileData);
                    buffer = findFileData.cFileName;
                    filelist.push_back(buffer);;//save file list in vector
                }
                return filelist;
    }

    int main()
    {
       string path;

       path="C:\\Documents and Settings\\OJT\\My Documents\\game\\FSNPC\\*.*";// the directory
       vector <string> handler;
       handler = GetPath(path);
        for (unsigned i=1;i<handler.size()-1;i++)
        {
            cout << handler[i]<<endl;//print out the vector
        }
       _getch();
    }

【问题讨论】:

    标签: c++ windows winapi filetime


    【解决方案1】:

    这是针对 *nix 平台的。

       int stat(const char *path, struct stat *buf);
       int fstat(int fd, struct stat *buf);
       int lstat(const char *path, struct stat *buf);
    
           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 */
           };
    

    【讨论】:

      【解决方案2】:

      我觉得windows API有GetFileTime函数,查一下,好久没看到windows了。

      更新:查看herehere

      【讨论】:

      • 我如何使用 GetFileTime.. 每当我使用该功能时,它不会输出任何内容
      • 输出?为什么你期望它输出任何东西?
      • 我需要将文件时间保存到某个变量.. 那我该怎么做呢?对不起,我只是 C++ 的新手
      • 我回答中的第二个链接为您提供了一个示例。
      【解决方案3】:

      由于我们现在知道您使用的是 Windows,因此您正在寻找 GetFileTime function,它允许您检索任何文件或目录的创建、上次访问或上次修改日期和时间。它通过使用时间值填充指定为参数的FILETIME structure(s) 来做到这一点。

      BOOL WINAPI GetFileTime(
        __in       HANDLE hFile,                // handle to the file
        __out_opt  LPFILETIME lpCreationTime,   // FILETIME struct for creation time
        __out_opt  LPFILETIME lpLastAccessTime, // FILETIME struct for last access time
        __out_opt  LPFILETIME lpLastWriteTime   // FILETIME struct for last modification time
      );
      

      由于您只关心创建时间,因此您可以忽略最后两个参数。它们是可选的。您可以找到检索文件here on MSDN 的最后写入时间的完整示例。

      一旦您使用适当的值填充了FILETIME 结构,您可能希望使用FileTimeToSystemTime function 将该值转换为便于显示的格式。

      【讨论】:

        【解决方案4】:

        Windows,你说?使用GetFileTime。然后,您可能希望使用FileTimeToSystemTime 使其更易于管理(GetFileTime 实际上只返回相对于 1601 年 1 月 1 日的 64 位时间戳,FileTimeToSystemTime 将其转换为具有小时、分钟字段的结构、日、月等)。

        【讨论】:

          【解决方案5】:

          你不能。 AFAIK 不存储创建时间。至少在linux上。 您可以使用 fstat 来获取上次修改时间或上次访问时间以及我不记得的另一个时间戳。但不存储创建时间。

          也许您的操作系统可以提供创建时间。但为此,您必须更具体。

          【讨论】:

          • 嗯,他在 Windows 上,它有创建时间(我不知道是否确实是创建时间)。
          • 第一次发帖的时候没提。
          • 我没有抱怨,我评论了 ;-)
          猜你喜欢
          • 2018-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多