小编Mik*_*keT的帖子

循环遍历文件并在C中打印文件属性

我是C语言编程的新手.我需要这个程序遍历文件夹中的所有文件并为每个文件打印这些属性.此时它正在打印文件夹的属性.

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

int main(int argc, char *argv[])
{
    DIR *dp;
    struct stat file_stats;

    if (argc != 2) {
        fprintf(stderr, "Usage: fstat FILE...\n");
        return EXIT_FAILURE;
    }

    if ((stat(argv[1], &file_stats)) == -1) {
        perror("fstat");
        return EXIT_FAILURE;
    }

    dp = opendir("./");
    if (dp == NULL) {
        perror("couldn't open directory");
        return EXIT_FAILURE;
    }

    while (readdir(dp)) {
        printf("filename: %s\n", argv[1]);
        printf(" device: %lld\n",
                file_stats.st_dev);
        printf(" protection: %o\n",
                file_stats.st_mode);
        printf(" number of hard links: %d\n", …
Run Code Online (Sandbox Code Playgroud)

c linux posix while-loop

5
推荐指数
1
解决办法
641
查看次数

标签 统计

c ×1

linux ×1

posix ×1

while-loop ×1