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:

</>
Copy
#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:

  1. #include <stdio.h> is used for standard input/output functions.
  2. #include <sys/stat.h> provides the stat() function to get file attributes.
  3. #include <time.h> is included to work with date and time functions.
  4. The stat() function retrieves the attributes of example.txt and stores them in the attr structure.
  5. The attr.st_mtime member contains the last modification time, which is converted to local time using localtime().
  6. strftime() formats the struct tm data into a readable string format.
  7. 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:

</>
Copy
#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:

  1. #include <stdio.h> is used for input/output functions, and #include <windows.h> is required for Windows API functions.
  2. CreateFile() opens example.txt with read access and returns a file handle (hFile).
  3. GetFileTime() retrieves the file times, including the last write time (ftWrite).
  4. FileTimeToSystemTime() converts the FILETIME structure to a SYSTEMTIME structure (stUTC) in Coordinated Universal Time (UTC).
  5. SystemTimeToTzSpecificLocalTime() converts the UTC SYSTEMTIME to local SYSTEMTIME (stLocal).
  6. 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:

  1. Using stat() (POSIX): Retrieves file attributes using the stat() function, converts the modification time to a readable format, and prints it.
  2. Using Windows API GetFileTime(): Opens the file using CreateFile(), retrieves the file times with GetFileTime(), converts the last write time to local time, and displays the formatted date.