27_目錄IO

爱吃冰激凌的黄某某發表於2024-04-10

目錄IO

檔案 IO 和目錄 IO 的對比:

image-20240404155211741

區別:

​ 之前我們學習的檔案 IO 和提到過的標準 IO 都是對檔案操作, 接下來學習的目錄 IO 都是對目錄操作。

mkdir

建立目錄函式如下表所示:

image-20240404155248246

程式碼

mkdir.c

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char const *argv[])
{
    int ret;

    if(argc != 2)
    {
        printf("Usage: %s <name file>\n", argv[0]);
        return -1;
    }

    ret = mkdir(argv[1], 0666);

    if(ret < 0)
    {
        printf("mkdir is error!!!\n");
        return -2;
    }
    printf("mkdir is ok!!!\n");
    return 0;
}

執行結果

image-20240404160954108

opendir/closedir

image-20240404161041536

程式碼

mkdir.c

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

int main(int argc, char const *argv[])
{
    int ret;
    DIR *dp;

    dp = opendir(argv[1]);

    if(dp != NULL)
    {
        printf("opendir is ok!!!\n");
    }
    
    closedir(dp);
    return 0;
}

執行結果

image-20240404161823854

readdir

image-20240404162032234

程式碼

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

int main(int argc, char const *argv[])
{
    int ret;
    DIR *dp;
    struct dirent *dir;

    dp = opendir(argv[1]);

    if (dp == NULL)
    {
        printf("opendir is error!!!\n");
        return -1;
    }
    printf("opendir is ok!!!\n");

    while (1)
    {
        dir = readdir(dp);
        if (dir != NULL)
        {
            printf("file name is %s\n", dir->d_name);
        }else
        {
            break;
        }
        
    }

    closedir(dp);

    return 0;
}

執行結果

image-20240404163500779

綜合練習

需求:

​ 1.列印我們要複製的目錄下的所有檔名, 並複製我們需要的檔案。
​ 2.透過鍵盤輸入我們要複製的檔案的路徑和檔名等資訊

程式碼

practice.c

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

int main(int argc, char const *argv[])
{
    DIR *dir;
    struct dirent *rdir;
    int fd_src, fd_obj;
    ssize_t ret;
    char buf[100] = {0};
    char file_path[32]={0};
    char file_name[32]={0};

    printf("please enter the file path:\n");
    scanf("%s", file_path);

    dir = opendir(file_path);
    if (dir == NULL)
    {
        printf("opendir is error!!!\n");
        return -2;
    }
    printf("opendir is ok!!!\n");
    while (1)
    {
        rdir = readdir(dir);
        if (rdir != NULL)
        {
            printf("filename: %s\n", rdir->d_name);
        }
        else
        {
            break;
        }
    }

    printf("please enter the file name:\n");
    scanf("%s", file_name);

    fd_src = open(strcat(strcat(file_path, "/"), file_name), O_RDONLY);
    fd_obj = open(file_name, O_CREAT | O_WRONLY, 0666);

    if (fd_src < 0 || fd_obj < 0)
    {
        printf("open is error!!!\n");
        return -3;
    }

    while ((ret = read(fd_src, buf, 32)) != 0)
    {
        write(fd_obj, buf, ret);
    }

    close(fd_obj);
    close(fd_src);
    closedir(dir);

    return 0;
}

執行結果

image-20240404183857209