目录列表 C(使用 dirent 和 stat)

发布于 2025-01-15 03:27:57 字数 1137 浏览 2 评论 0原文

我正在制作一个打印目录列表应用程序 目录列表就像 Linux 和 Windows 中的“ls”和“dir”命令一样 积极地。

我的功能:打印路径指定的目录中所有文件的列表。

这是我到目前为止的代码:

#include "ls.h"

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

// Use this function to display the files. DO NOT CHANGE IT.
void _printLine(unsigned int size, unsigned int sizeOnDisk, const char* name)
{
    printf("%010u   %010u   %s\n", size, sizeOnDisk, name);
}

// Assume this to be the maximum length of a file name returned by readdir
#define MAX_FILE_NAME_LENGTH 255

int list(const char* path)
{
    (void) path;
    struct dirent *dent;
    struct stat s;
    DIR *dir;
    dir = opendir(".");
    if (!dir){
        perror("opendir");
        return -1;
    }

    errno = 0;
    while ((dent = readdir(dir)) != NULL){
        _printLine(s.st_size, s.st_blocks*512, dent->d_name);
    }
    closedir(dir);

    return 0;
}

我试图将文件的“大小”和“磁盘上的大小”传递给打印函数(使用 stat),同时还传递文件的名称(使用 dirent)。但我不知道如何实现这一权利,或者是否可能?

Im making a directory listing application that prints a
directory listing just like the ’ls’ and ’dir’ commands in Linux and Windows respec-
tively.

my function: prints a listing of all files in the directory specified by path.

this is my code so far:

#include "ls.h"

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

// Use this function to display the files. DO NOT CHANGE IT.
void _printLine(unsigned int size, unsigned int sizeOnDisk, const char* name)
{
    printf("%010u   %010u   %s\n", size, sizeOnDisk, name);
}

// Assume this to be the maximum length of a file name returned by readdir
#define MAX_FILE_NAME_LENGTH 255

int list(const char* path)
{
    (void) path;
    struct dirent *dent;
    struct stat s;
    DIR *dir;
    dir = opendir(".");
    if (!dir){
        perror("opendir");
        return -1;
    }

    errno = 0;
    while ((dent = readdir(dir)) != NULL){
        _printLine(s.st_size, s.st_blocks*512, dent->d_name);
    }
    closedir(dir);

    return 0;
}

Im trying to pass the "size" of the file and "size on disk" to the print function(using stat), while also passing the name of the file (using dirent). But i can't figure out how to implement this right, or if it is even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2025-01-22 03:27:57

您从未调用过stat

$ gcc -Wall ls.c 
$ ./a.out .
0000000160   0000000000   .
0000000608   0000000000   ..
0000000947   0000004096   ls.c
0000000811   0000004096   read.c
0000049840   0000053248   a.out
$ ls -l 
total 120
-rwxr-xr-x  1 anicolao  wheel  49840 Mar 18 08:39 a.out
-rw-r--r--  1 anicolao  wheel    947 Mar 18 08:39 ls.c
-rw-r--r--  1 anicolao  wheel    811 Mar 17 17:58 read.c
$ cat ls.c

//#include "ls.h"

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

// Use this function to display the files. DO NOT CHANGE IT.
void _printLine(unsigned int size, unsigned int sizeOnDisk, const char* name)
{
    printf("%010u   %010u   %s\n", size, sizeOnDisk, name);
}

// Assume this to be the maximum length of a file name returned by readdir
#define MAX_FILE_NAME_LENGTH 255

int list(const char* path)
{
    (void) path;
    struct dirent *dent;
    struct stat s;
    DIR *dir;
    dir = opendir(".");
    if (!dir){
        perror("opendir");
        return -1;
    }

    errno = 0;
    while ((dent = readdir(dir)) != NULL){
        stat(dent->d_name, &s);
        _printLine(s.st_size, s.st_blocks*512, dent->d_name);
    }
    closedir(dir);

    return 0;
}

int main(int argc, char **argv) {
    return list(argv[1]);
}

还缺少错误检查,但我认为缺少的调用会让您继续前进。

You never called stat?

$ gcc -Wall ls.c 
$ ./a.out .
0000000160   0000000000   .
0000000608   0000000000   ..
0000000947   0000004096   ls.c
0000000811   0000004096   read.c
0000049840   0000053248   a.out
$ ls -l 
total 120
-rwxr-xr-x  1 anicolao  wheel  49840 Mar 18 08:39 a.out
-rw-r--r--  1 anicolao  wheel    947 Mar 18 08:39 ls.c
-rw-r--r--  1 anicolao  wheel    811 Mar 17 17:58 read.c
$ cat ls.c

//#include "ls.h"

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

// Use this function to display the files. DO NOT CHANGE IT.
void _printLine(unsigned int size, unsigned int sizeOnDisk, const char* name)
{
    printf("%010u   %010u   %s\n", size, sizeOnDisk, name);
}

// Assume this to be the maximum length of a file name returned by readdir
#define MAX_FILE_NAME_LENGTH 255

int list(const char* path)
{
    (void) path;
    struct dirent *dent;
    struct stat s;
    DIR *dir;
    dir = opendir(".");
    if (!dir){
        perror("opendir");
        return -1;
    }

    errno = 0;
    while ((dent = readdir(dir)) != NULL){
        stat(dent->d_name, &s);
        _printLine(s.st_size, s.st_blocks*512, dent->d_name);
    }
    closedir(dir);

    return 0;
}

int main(int argc, char **argv) {
    return list(argv[1]);
}

There is also missing error checking, but I think the missing call will get you going.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文