Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get a file's modified date/time and size in bytes?
#14
(05-22-2024, 06:42 PM)SpriggsySpriggs Wrote:
Quote:Sure, let's create a function in C that retrieves the last modified date and time of a file in a cross-platform manner. We'll use the standard library functions available in C for both Unix-like systems (Linux, macOS) and Windows.

### Plan

1. **Include necessary headers**: We need to include different headers depending on the platform.
2. **Define the function**: The function `get_last_modified_time` will take a file path as an argument.
3. **Platform-specific code**:
  - For Unix-like systems, we'll use `stat` to get the file's metadata.
  - For Windows, we'll use `GetFileAttributesEx` to get the file's metadata.
4. **Format the output**: We'll format the output to display the last modified date and time.

Here’s the complete C code:

```c
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#endif

void get_last_modified_time(const char *filepath) {
#ifdef _WIN32
    WIN32_FILE_ATTRIBUTE_DATA fileInfo;
    if (GetFileAttributesEx(filepath, GetFileExInfoStandard, &fileInfo)) {
        FILETIME ft = fileInfo.ftLastWriteTime;
        SYSTEMTIME st;
        FileTimeToSystemTime(&ft, &st);
        printf("Last modified date and time: %02d/%02d/%d %02d:%02d:%02d\n",
              st.wDay, st.wMonth, st.wYear,
              st.wHour, st.wMinute, st.wSecond);
    } else {
        printf("Failed to get file attributes for %s\n", filepath);
    }
#else
    struct stat attr;
    if (stat(filepath, &attr) == 0) {
        struct tm *tm_info = localtime(&attr.st_mtime);
        char buffer[30];
        strftime(buffer, 30, "%Y-%m-%d %H:%M:%S", tm_info);
        printf("Last modified date and time: %s\n", buffer);
    } else {
        perror("stat");
    }
#endif
}

int main() {
    const char *filepath = "path/to/your/file.txt";
    get_last_modified_time(filepath);
    return 0;
}
```

### Explanation

1. **Headers**:
  - `stdio.h` for input/output functions.
  - `time.h` for time manipulation.
  - `windows.h` for Windows-specific file attributes.
  - `sys/stat.h` for Unix-like system file attributes.

2. **Function**:
  - `get_last_modified_time` is defined to handle both Unix-like and Windows systems.
  - On Windows, `GetFileAttributesEx` is used to get file information and `FileTimeToSystemTime` to convert it to a readable format.
  - On Unix-like systems, `stat` is used to get file information and `localtime` to convert the last modification time to a readable format.

3. **Main Function**:
  - Example usage with a placeholder file path.
  - Call to `get_last_modified_time` to print the last modified date and time.

### Suggestions for Next Steps

**a.** Add error handling for different edge cases (e.g., file not found, permission issues).

**b.** Modify the function to return the last modified date and time as a string instead of printing it.

No big deal, but since QB64PE uses gcc/g++ for Windows, you don't need the "#ifdef WIN32" part, since stat() also works on Windows.
For instance, this:
Code: (Select All)
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>

void get_last_modified_time(const char *filepath) {
    struct stat attr;
    if (stat(filepath, &attr) == 0) {
        struct tm *tm_info = localtime(&attr.st_mtime);
        char buffer[30];
        strftime(buffer, 30, "%Y-%m-%d %H:%M:%S", tm_info);
        printf("Last modified date and time: %s\n", buffer);
    } else {
        perror("stat");
    }
}

int main(int argc, char *argv[]) {
    const char *filepath = argv[1];
    if (argc < 2) {
        printf("Usage: foo filename\n");
    } else {
        get_last_modified_time(filepath);
    }
    return 0;
}
Compiles and runs under Windows.

Again, no big deal, just a friendly FYI.
Reply


Messages In This Thread
RE: how to get a file's modified date/time and size in bytes? - by Ed Davis - 05-22-2024, 07:58 PM



Users browsing this thread: 4 Guest(s)