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

我有一个在获得文件时间创build问题..谁能知道如何获得文件时间?

我有一个代码,得到一个特定的目录的文件名我的问题是我不知道如何把文件放在目录中的每个文件…

这里是我的代码:

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

我认为Windows API有GetFileTime函数,查看它,我有一段时间没有看到Windows。

更新:看这里和这里

这是* 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获取上次修改时间或上次访问时间以及另一个我不记得的时间戳记。 但创建时间不存储。

也许你的操作系统可以给创造时间。 但为此,你必须更具体。

既然我们现在知道你在Windows上,你正在寻找GetFileTime函数 ,它可以让你检索任何文件或目录的创建,上次访问或上次修改日期和时间。 它通过用指定的FILETIME结构作为参数来填充时间值。

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

由于您关心的只是创建时间,所以您可以忽略最后两个参数。 他们是可选的。 您可以在MSDN上找到一个完整的样本来检索文件的最后写入时间。

一旦你得到FILETIME结构填入适当的值,你可能会想要使用FileTimeToSystemTime函数将该值转换成一个友好的显示格式。

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