溫馨提示×

如何檢查copendir是否成功

小樊
46
2025-06-09 21:01:14
欄目: 編程語言

在C語言中,copdir函數用于復制目錄及其內容。然而,C標準庫中并沒有直接提供copdir函數。你可能是指opendirreaddir函數來遍歷目錄,以及mkdir函數來創建目錄。如果你想要檢查復制目錄操作是否成功,你需要自己實現這個功能。

以下是一個簡單的示例,展示了如何檢查復制目錄操作是否成功:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int copy_directory_contents(const char *src, const char *dst) {
    DIR *dir = opendir(src);
    if (dir == NULL) {
        perror("opendir");
        return -1;
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue; // Skip current and parent directory entries
        }

        char src_path[PATH_MAX];
        snprintf(src_path, sizeof(src_path), "%s/%s", src, entry->d_name);

        char dst_path[PATH_MAX];
        snprintf(dst_path, sizeof(dst_path), "%s/%s", dst, entry->d_name);

        struct stat st;
        if (stat(src_path, &st) == -1) {
            perror("stat");
            closedir(dir);
            return -1;
        }

        if (S_ISDIR(st.st_mode)) {
            // Directory, create it in the destination
            if (mkdir(dst_path, st.st_mode) != 0) {
                perror("mkdir");
                closedir(dir);
                return -1;
            }
        } else {
            // File, copy it
            FILE *src_file = fopen(src_path, "rb");
            if (src_file == NULL) {
                perror("fopen");
                closedir(dir);
                return -1;
            }

            FILE *dst_file = fopen(dst_path, "wb");
            if (dst_file == NULL) {
                perror("fopen");
                fclose(src_file);
                closedir(dir);
                return -1;
            }

            char buffer[4096];
            size_t bytes_read;
            while ((bytes_read = fread(buffer, 1, sizeof(buffer), src_file)) > 0) {
                if (fwrite(buffer, 1, bytes_read, dst_file) != bytes_read) {
                    perror("fwrite");
                    fclose(src_file);
                    fclose(dst_file);
                    closedir(dir);
                    return -1;
                }
            }

            fclose(src_file);
            fclose(dst_file);
        }
    }

    closedir(dir);

    // Copy directory attributes
    if (chmod(dst, st.st_mode) != 0) {
        perror("chmod");
        return -1;
    }

    return 0;
}

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <source_directory> <destination_directory>\n", argv[0]);
        return EXIT_FAILURE;
    }

    const char *src_dir = argv[1];
    const char *dst_dir = argv[2];

    if (copy_directory_contents(src_dir, dst_dir) == 0) {
        printf("Directory copied successfully.\n");
    } else {
        fprintf(stderr, "Failed to copy directory.\n");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

這個程序接受兩個命令行參數:源目錄和目標目錄。它會遍歷源目錄中的所有文件和子目錄,并將它們復制到目標目錄中。如果在復制過程中遇到任何錯誤,它會打印出相應的錯誤消息并返回-1。如果復制成功,它將返回0。

請注意,這個程序沒有處理符號鏈接、設備文件、命名管道等其他特殊文件類型。如果你需要處理這些類型,你需要添加額外的邏輯來檢查文件類型并相應地處理它們。此外,這個程序也沒有處理權限問題,如果目標目錄沒有足夠的權限,復制操作可能會失敗。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女