在Linux中,copendir()
函數用于復制一個目錄
要獲取目錄的修改時間,您可以使用stat()
函數。以下是一個示例代碼,展示了如何使用copendir()
和stat()
函數獲取目錄的修改時間:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <source_dir> <destination_dir>\n", argv[0]);
return EXIT_FAILURE;
}
const char *source_dir = argv[1];
const char *destination_dir = argv[2];
// 打開源目錄
DIR *source_dir_handle = opendir(source_dir);
if (!source_dir_handle) {
perror("Failed to open source directory");
return EXIT_FAILURE;
}
// 復制源目錄到目標目錄
if (copendir(source_dir, destination_dir) == NULL) {
perror("Failed to copy directory");
closedir(source_dir_handle);
return EXIT_FAILURE;
}
// 獲取源目錄的修改時間
struct stat source_dir_stat;
if (stat(source_dir, &source_dir_stat) == -1) {
perror("Failed to get source directory stats");
closedir(source_dir_handle);
return EXIT_FAILURE;
}
// 輸出源目錄的修改時間
printf("Source directory '%s' modified on: %s", source_dir, ctime(&source_dir_stat.st_mtime));
// 關閉源目錄
closedir(source_dir_handle);
return EXIT_SUCCESS;
}
在這個示例中,我們首先檢查命令行參數的數量,然后使用copendir()
函數復制源目錄到目標目錄。接下來,我們使用stat()
函數獲取源目錄的修改時間,并使用ctime()
函數將其轉換為可讀的字符串格式。最后,我們輸出源目錄的修改時間并關閉源目錄。