OneCompiler

slip12&slip24

131

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>

void listFiles(const char *dirPath, int targetMonth) {
DIR *dir;
struct dirent *entry;
struct stat fileStat;

dir = opendir(dirPath);
if (dir == NULL) {
    perror("Error opening directory");
    return;
}

while ((entry = readdir(dir)) != NULL) {
    // Construct the full path of the file/directory
    char fullPath[1024];
    snprintf(fullPath, sizeof(fullPath), "%s/%s", dirPath, entry->d_name);

    if (stat(fullPath, &fileStat) == -1) {
        perror("Error");
        continue;
    }

    if (S_ISREG(fileStat.st_mode)) {
        struct tm *timeInfo = localtime(&fileStat.st_ctime);
        int fileMonth = timeInfo->tm_mon + 1;  // Months are zero-based

        if (fileMonth == targetMonth) {
            printf("%s\n", fullPath);
        }
    }
}

closedir(dir);

}
int main() {
int targetMonth;

printf("Enter the target month (1-12): ");
scanf("%d", &targetMonth);

if (targetMonth < 1 || targetMonth > 12) {
    printf("Invalid month\n");
    return 1;
}

listFiles(".", targetMonth);

return 0;

}