【问题标题】:c program to list content directory with all attributesc程序列出具有所有属性的内容目录
【发布时间】:2021-06-22 00:31:57
【问题描述】:

当前的大学学生,需要帮助编写一个 c 程序来显示目录中的内容和所有信息。

列表应包含文件名、模式、链接、用户、组大小和修改时间。

这是我已经完成的:

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

void do_ls(char [], struct stat *);
void show_stat_info(char [], struct stat *);

main(int ac, char *av[])
{
    struct stat info;   
    if ( ac == 1 )
        do_ls( ".",&info );
    else
        while ( --ac ){
            printf("%s:\n", *++av );
            do_ls( *av,&info );
        }
}

void do_ls( char dirname[], struct stat *buf )
/*
 *  list files in directory called dirname
 */
{
    DIR     *dir_ptr;       /* the directory */
    struct dirent   *direntp;       /* each entry    */

    if ( ( dir_ptr = opendir( dirname ) ) == NULL )
        fprintf(stderr,"ls1: cannot open %s\n", dirname);
    else
    {
        while ( ( direntp = readdir( dir_ptr ) ) != NULL )
        {
            printf("%s\n", direntp->d_name );
            printf("   mode: %o\n", buf->st_mode);         /* type + mode */
            printf("  links: %d\n", buf->st_nlink);        /* # links     */
            printf("   user: %d\n", buf->st_uid);          /* user id     */
            printf("  group: %d\n", buf->st_gid);          /* group id    */
            printf("   size: %d\n", buf->st_size);         /* file size   */
            printf("modtime: %d\n", buf->st_mtime);        /* modified    */
            //printf("   name: %s\n", fname );               /* filename    */
        }
        closedir(dir_ptr);
    }
}

现在它只列出目录中的文件,但所有细节都归零 当我将目录传递给函数时,它会说“无法打开文件名”

这就是我想要达到的目标:

如果没有传递参数,程序将打印当前目录的所有名称和详细信息

如果通过一个目录,程序将打印目录内所有文件的信息

【问题讨论】:

  • 你还没有在任何地方打电话给stat。所以buf 未初始化。
  • 除了你自己的错误信息我建议添加perror("opendir")打印系统错误信息或使用strerror将系统错误信息整合到你自己的信息中。请在您的问题中显示您拥有哪些目录和文件,您如何准确地调用您的程序并复制并粘贴确切的输出或错误消息。当您调用程序时,您的当前目录是什么?在您的解释中,您提到了一条错误消息“无法打开文件名”。您是指fprintf(stderr,"ls1: cannot open %s\n", dirname); 打印的略有不同的消息吗?
  • 您实际上不需要将struct stat *buf 作为参数传递。您可以在do_ls() 中声明struct stat info;,然后调用stat (direntp-&gt;d_name, &amp;info); 来填充每个文件名的结构。您实际上可以在 while ( ( direntp = readdir( dir_ptr ) ) != NULL ) 循环中声明它。

标签: c unix


【解决方案1】:

我去掉了stat指针参数,使用early return来简化代码,并在通过组合目录和文件名构建的每个路径上调用stat(粗略),还修复了格式字符串:

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

void do_ls(char dirname[]) {
    DIR *dir_ptr;
    if(!(dir_ptr = opendir( dirname ))) {
        fprintf(stderr,"ls1: cannot open %s\n", dirname);
        return;
    }
    struct dirent *direntp;
    while((direntp = readdir(dir_ptr))) {
        struct stat buf;
        char path[PATH_MAX];
        snprintf(path, PATH_MAX, "%s/%s", dirname, direntp->d_name);
        if(stat(path, &buf)) {
           fprintf(stderr,"stat failed\n");
           break;
        }
        printf("%s\n", direntp->d_name );
        printf("   mode: %o\n", buf.st_mode);         /* type + mode */
        printf("  links: %ld\n", buf.st_nlink);        /* # links     */
        printf("   user: %d\n", buf.st_uid);          /* user id     */
        printf("  group: %d\n", buf.st_gid);          /* group id    */
        printf("   size: %ld\n", buf.st_size);         /* file size   */
        printf("modtime: %ld\n", buf.st_mtime);        /* modified    */
        printf("   name: %s\n", path );               /* filename    */
    }
    closedir(dir_ptr);
}

int main(int ac, char *av[]) {
    if(ac == 1) {
        do_ls(".");
        return 0;
    }
    while(--ac) {
        printf("%s:\n", *++av);
        do_ls(*av);
    }
    return 0;
}

【讨论】:

  • 这给了我一个失败的统计数据
  • 当我在统计数据上使用 perror 它给了我成功>
  • 实际上,当我删除对 stat 的检查时,代码可以正常工作
  • 我设法让您的代码正常工作,但是当我通过命令行中的特定目录时,它说该目录无法访问?
  • 对不起,关于那个,我在测试代码并反转测试后添加了错误检查。固定的。它适用于我,没有指定参数或目录。
猜你喜欢
  • 2015-08-15
  • 2015-05-24
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多