学习笔记4 截图+代码

学习笔记4

一、苏格拉底挑战











二、学习时遇见的问题利用gpt解决



三、实践stat 和opendir-readdir



四、实践代码

使用stat结构体:

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

    int main() {
        struct stat fileStat;

        // 获取文件属性
        if (stat("myfile.txt", &fileStat) == 0) {
            printf("File Size: %ld bytes\n", fileStat.st_size);
            printf("Number of Links: %ld\n", fileStat.st_nlink);
            printf("File Inode: %ld\n", fileStat.st_ino);
            printf("Owner UID: %d\n", fileStat.st_uid);
            printf("Owner GID: %d\n", fileStat.st_gid);
            printf("File Type and Mode: %o\n", fileStat.st_mode);
            printf("Last Access Time: %ld\n", fileStat.st_atime);
            printf("Last Modification Time: %ld\n", fileStat.st_mtime);
            printf("Last Status Change Time: %ld\n", fileStat.st_ctime);
        } else {
            perror("stat");
        }

        return 0;
    }

获取文件索引节点:

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

int main() {
    struct stat fileStat;

    // 获取文件属性
    if (stat("myfile.txt", &fileStat) == 0) {
        printf("File Inode: %ld\n", fileStat.st_ino);
    } else {
        perror("stat");
    }

    return 0;
}

Opendir:

#include <stdio.h>
#include <dirent.h>

int main() {
    DIR *dir;
    struct dirent *entry;

    dir = opendir("/home/yuanyi/study");
    if (dir == NULL) {
        perror("opendir");
        return 1;
    }

    while ((entry = readdir(dir))) {
        printf("%s\n", entry->d_name);
    }

    closedir(dir);
    return 0;
}

readlink示例:

#include <stdio.h>
#include <unistd.h>

int main() {
    char linkname[1024];
    ssize_t len;

    // 读取符号链接的目标路径
    len = readlink("mysymlink", linkname, sizeof(linkname) - 1);
    if (len != -1) {
        linkname[len] = '\0';  // 添加字符串终止符
        printf("Symbolic Link Target: %s\n", linkname);
    } else {
        perror("readlink");
        return 1;
    }

    return 0;
}
posted @ 2023-09-30 23:09  20211423袁艺  阅读(14)  评论(0编辑  收藏  举报