Outputting date and time a file was modified C++

AgentZap

Senior member
Sep 1, 2001
730
0
0
I am working in a console app that reads out of a file. Is there a prebuilt function that will output the date and time the file was last modified from within the program?

This is for C++. Thanks.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
What platform?

On unix, use fstat or stat (fstat sounds more appropriate for your situation... although if you're using C++ i/o, then you won't have a fd)
 

singh

Golden Member
Jul 5, 2001
1,449
0
0

Or rather, something like this: http://msdn.microsoft.com/library/defau...ase/retrieving_the_last_write_time.asp


Modify the example to:
1. Open the file using the CreateFile() API call. For instance,

string fileName = "c:\\example.txt";
HANDLE hFile = CreateFile(fileName.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);


2. Check the return values to catch non-existent or inaccessable files.
3. Call CloseFile() on the file handle obtained from CreateFile() once you're finished.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
There is a Windows class that allows you to retrieve file related info

CFile::GetStatus(CFileStatus &rStatus)

CFileStatus structure components


CTime m_ctime - creation
CTime m_mtime - last modification
CTime m_atime - last accessed
long m_size - logical size in bytes
byte m_attribute - attribute byte of the file
char m_szFullName[_MaxSize] absolute filename
 

AgentZap

Senior member
Sep 1, 2001
730
0
0
Originally posted by: EagleKeeper
There is a Windows class that allows you to retrieve file related info

CFile::GetStatus(CFileStatus &rStatus)

CFileStatus structure components


CTime m_ctime - creation
CTime m_mtime - last modification
CTime m_atime - last accessed
long m_size - logical size in bytes
byte m_attribute - attribute byte of the file
char m_szFullName[_MaxSize] absolute filename

How would one envoke something like that on something like C:\blah.txt
 

AgentZap

Senior member
Sep 1, 2001
730
0
0
Originally posted by: singh

Or rather, something like this: http://msdn.microsoft.com/library/defau...ase/retrieving_the_last_write_time.asp


Modify the example to:
1. Open the file using the CreateFile() API call. For instance,

string fileName = "c:\\example.txt";
HANDLE hFile = CreateFile(fileName.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);


2. Check the return values to catch non-existent or inaccessable files.
3. Call CloseFile() on the file handle obtained from CreateFile() once you're finished.

Can this work with using an API call? I need to open the file using fstream ios::in and ios::eek:ut etc in my application.