windows 如何在 C++ 中获取文件时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5178403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get File Time in C++
提问by Andro Miguel M. Bondoc
i am having a problem in getting the File Time Creation.. could anyone know how to get the FILE time??
我在获取文件时间创建时遇到问题.. 谁能知道如何获取文件时间?
I got a code that gets the file name of a certain directory my problem is i dont know how to put the filetimes on each file in the directory...
我得到了一个代码来获取某个目录的文件名我的问题是我不知道如何将文件时间放在目录中的每个文件上...
here is my code:
这是我的代码:
#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();
}
回答by CyberDem0n
This is for *nix platforms.
这是针对 *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 */
};
回答by Michael Krelin - hacker
回答by Cody Gray
Since we know now that you're on Windows, you're looking for the GetFileTime
function, which allows you to retrieve the creation, last access, or last modified date and time for any file or directory. It does this by filling the FILETIME
structure(s)specified as arguments with the time values.
由于我们现在知道您使用的是 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
);
Since all you care about is the creation time, you can ignore the last two parameters. They are optional. You can find a complete sample of retrieving the last write time of a file here on MSDN.
由于您只关心创建时间,因此您可以忽略最后两个参数。它们是可选的。您可以在 MSDN 上找到检索文件上次写入时间的完整示例。
Once you get the FILETIME
structure filled in with the appropriate value, you will probably want to use the FileTimeToSystemTime
functionto convert that value into a display-friendly format.
一旦FILETIME
用适当的值填充结构,您可能希望使用该FileTimeToSystemTime
函数将该值转换为显示友好的格式。
回答by Mac
Windows, you say? Use GetFileTime. You'll then probably want to use FileTimeToSystemTimeto make it a little more manageable (GetFileTime
effectively just returns a 64-bit timestamp relative to 01 January 1601, FileTimeToSystemTime
converts that to a structure with fields for hours, minutes, day, month, etc...).
窗户,你说?使用GetFileTime。然后,您可能希望使用FileTimeToSystemTime使其更易于管理(GetFileTime
实际上只是返回相对于 1601 年 1 月 1 日的 64 位时间戳,FileTimeToSystemTime
将其转换为具有小时、分钟、日、月等字段的结构。 .)
回答by mkaes
You can't. AFAIK the creation time is not stored. At least on linux. You can use fstat to get the last modififed time or the last access time and one more time stamp that I can't remember. But the creation time is not stored.
你不能。AFAIK 不存储创建时间。至少在 linux 上。您可以使用 fstat 获取上次修改时间或上次访问时间以及一个我不记得的时间戳。但不存储创建时间。
Maybe your OS can give the creation time. But for this you must be more specific.
也许您的操作系统可以提供创建时间。但为此,您必须更加具体。