Get File Modification Date in C
To get the file modification date in C, you can try these approaches: one using the standard stat()
function (suitable for POSIX systems) and another using the Windows API function GetFileTime()
for Windows systems.
Example 1: Using stat()
to Retrieve File Modification Date
In this example, we will use the POSIX stat()
function to obtain the file modification date of a file named example.txt
. We then convert the modification time to a human-readable format using localtime()
and strftime()
.
Explanation:
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
int main() {
struct stat attr;
// Retrieve file attributes of "example.txt"
if (stat("example.txt", &attr) == 0) {
// Convert modification time to local time
struct tm *timeinfo = localtime(&attr.st_mtime);
char buffer[80];
// Format the date as "YYYY-MM-DD HH:MM:SS"
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
printf("Last modification date: %s\n", buffer);
} else {
perror("stat");
}
return 0;
}
Explanation:
#include <stdio.h>
is used for standard input/output functions.#include <sys/stat.h>
provides thestat()
function to get file attributes.#include <time.h>
is included to work with date and time functions.- The
stat()
function retrieves the attributes ofexample.txt
and stores them in theattr
structure. - The
attr.st_mtime
member contains the last modification time, which is converted to local time usinglocaltime()
. strftime()
formats thestruct tm
data into a readable string format.- Finally, the formatted date is printed using
printf()
.
Output:
Last modification date: 2023-08-20 15:30:45
Example 2: Using Windows API GetFileTime()
to Retrieve File Modification Date
In this example, we will use the Windows API functions to obtain the file modification date for example.txt
. We open the file using CreateFile()
, retrieve its file times with GetFileTime()
, and convert the last write time to local time for display.
Explanation:
#include <stdio.h>
#include <windows.h>
int main() {
// Open the file "example.txt" for reading
HANDLE hFile = CreateFile("example.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("Could not open file.\n");
return 1;
}
FILETIME ftCreate, ftAccess, ftWrite;
// Retrieve file times (creation, last access, and last write)
if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite)) {
SYSTEMTIME stUTC, stLocal;
// Convert the last write time from FILETIME to SYSTEMTIME in UTC
FileTimeToSystemTime(&ftWrite, &stUTC);
// Convert UTC SYSTEMTIME to local SYSTEMTIME
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
printf("Last modification date: %04d-%02d-%02d %02d:%02d:%02d\n",
stLocal.wYear, stLocal.wMonth, stLocal.wDay,
stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
} else {
printf("Failed to get file time.\n");
}
CloseHandle(hFile);
return 0;
}
Explanation:
#include <stdio.h>
is used for input/output functions, and#include <windows.h>
is required for Windows API functions.CreateFile()
opensexample.txt
with read access and returns a file handle (hFile
).GetFileTime()
retrieves the file times, including the last write time (ftWrite
).FileTimeToSystemTime()
converts the FILETIME structure to a SYSTEMTIME structure (stUTC
) in Coordinated Universal Time (UTC).SystemTimeToTzSpecificLocalTime()
converts the UTC SYSTEMTIME to local SYSTEMTIME (stLocal
).- The formatted local time is then printed using
printf()
.
Output:
Last modification date: 2023-08-20 15:30:45
Conclusion
In this tutorial, we explored two different approaches to retrieve the file modification date in C:
- Using
stat()
(POSIX): Retrieves file attributes using thestat()
function, converts the modification time to a readable format, and prints it. - Using Windows API
GetFileTime()
: Opens the file usingCreateFile()
, retrieves the file times withGetFileTime()
, converts the last write time to local time, and displays the formatted date.