Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++中获取文件时间_C++_Windows_Winapi_Filetime - Fatal编程技术网

如何在C++中获取文件时间

如何在C++中获取文件时间,c++,windows,winapi,filetime,C++,Windows,Winapi,Filetime,我在获取文件创建时间时遇到问题。。有人知道如何获取文件时间吗 我得到了一个获取某个目录文件名的代码我的问题是我不知道如何在目录中的每个文件上放置filetimes 这是我的密码: #include <vector> #include <string> #include <windows.h> #include <stdio.h> #include <iostream> #include <conio.h> #include

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

我得到了一个获取某个目录文件名的代码我的问题是我不知道如何在目录中的每个文件上放置filetimes

这是我的密码:

#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();
    }

这是针对*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 */
       };

这是针对*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 */
       };

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


也许你的操作系统可以给你创造时间。但要做到这一点,你必须更加具体。

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

也许你的操作系统可以给你创造时间。但对于这一点,您必须更具体。

我认为windows API具有GetFileTime函数,请查找,我已经有一段时间没有看到windows了

更新:看和

我想windows API有GetFileTime函数,查一下,我已经有一段时间没看到windows了


更新:查找和

因为我们现在知道您在Windows上,所以您正在查找,它允许您检索任何文件或目录的创建、上次访问或上次修改日期和时间。它通过用时间值填充指定为参数来实现这一点

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
);
因为您只关心创建时间,所以可以忽略最后两个参数。它们是可选的。您可以找到检索文件上次写入时间的完整示例


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

因为我们现在知道您在Windows上,所以您正在查找,它允许您检索创建、上次访问、,或任何文件或目录的上次修改日期和时间。它通过用时间值填充指定为参数来实现这一点

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
);
因为您只关心创建时间,所以可以忽略最后两个参数。它们是可选的。您可以找到检索文件上次写入时间的完整示例


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

Windows,您说呢?使用然后,您可能希望使用使其更易于管理GetFileTime有效地返回一个相对于1601年1月1日的64位时间戳,FileTimeToSystemTime将其转换为一个包含小时、分钟、天、月等字段的结构…

Windows,您说?使用然后,您可能希望使用使其更易于管理GetFileTime有效地返回相对于1601年1月1日的64位时间戳,FileTimeToSystemTime将其转换为带有小时、分钟、天、月等字段的结构…

我如何使用GetFileTime。。每当我使用这个函数时,它不会输出任何东西?为什么您希望它输出任何东西?我需要将filetime保存到某个变量。。那我怎么能这么做??抱歉,我只是C++的新手。我回答中的第二个链接提供了一个示例。我如何使用GetFileTime。。每当我使用这个函数时,它不会输出任何东西?为什么您希望它输出任何东西?我需要将filetime保存到某个变量。。那我怎么能这么做??抱歉,我只是C++的新手。我回答中的第二个链接为您提供了一个示例。好吧,他在windows上,它有创建时间,不管它是否真的是创建时间,我不知道。当他第一次发布时,他没有提到它。我没有抱怨,我评论道-嗯,他在windows上,它有创建时间,不管是不是真的,创建时间,我不知道。当他第一次发布时,他没有提到它。我没有抱怨,我评论道-