如何知道一个文件最近什么时候被写过

acange 2003-08-21 12:19:55
如何知道一个文件最近什么时候被写过

比如我在c:\myfile.txt,我怎么知道他什么时候被写过。。。
谢谢了
...全文
32 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
oopig 2003-08-21
  • 打赏
  • 举报
回复
1.API:
BOOL GetFileTime(
HANDLE hFile,
LPFILETIME lpCreationTime,
LPFILETIME lpLastAccessTime,
LPFILETIME lpLastWriteTime
);
2.sample:
// GetLastWriteTime - retrieves the last-write time and converts the
// time to a string
// Return value - TRUE if successful, FALSE otherwise
// hFile - must be a valid file handle
// lpszString - pointer to buffer to receive string

BOOL GetLastWriteTime(HANDLE hFile, LPSTR lpszString)
{
FILETIME ftCreate, ftAccess, ftWrite;
SYSTEMTIME stUTC, stLocal;

// Retrieve the file times for the file.
if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
return FALSE;

// Convert the last-write time to local time.
FileTimeToSystemTime(&ftWrite, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

// Build a string showing the date and time.
wsprintf(lpszString, "%02d/%02d/%d %02d:%02d",
stLocal.wDay, stLocal.wMonth, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);

return TRUE;
}
skyxie 2003-08-21
  • 打赏
  • 举报
回复
如果不用API,


直接用
CFileStatus status;
//status.m_mtime;//CTime,最近修改时间
//status.m_ctime;//CTime,创建时间
//status.m_atime;//CTime,最近访问时间
//status.m_attribute;//BYTE,属性;==0x01,只读;==0x02,隐藏;==0x04,系统;==0x20,存档

if( CFile::GetStatus("c:\\myfile.txt",status) )
{
CTime tm = status.m_mtime;//这样就得到了文件的最近修改时间
}
zhouyong0371 2003-08-21
  • 打赏
  • 举报
回复
你的lpFileName只要等于:"C:\\myfile.txt"就可以了
zhouyong0371 2003-08-21
  • 打赏
  • 举报
回复
WIN32_FIND_DATA FindFileData;

FindClose(FindFirstFile(lpFileName,&FindFileData));

其中:

typedef struct _WIN32_FIND_DATA {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime; //看到了吧?
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
TCHAR cFileName[ MAX_PATH ];
TCHAR cAlternateFileName[ 14 ];
} WIN32_FIND_DATA, *PWIN32_FIND_DATA;




akiko 2003-08-21
  • 打赏
  • 举报
回复
CFileStatus cfs;
CFile::GetStatus("c:\\myfile.txt",cfs);
CString str=cfs.m_mtime.Format("%Y年%B月%d日%H点%M分%S秒");
AfxMessageBox(str);

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧