【问题标题】:Print out file names and its' sizes in C在 C 中打印出文件名及其大小
【发布时间】:2013-01-28 18:58:46
【问题描述】:

我不确定 C 是否可以做到这一点,但我希望我可以制作一个程序来查看目录,并打印出目录的所有内容以及每个文件的文件大小.因为我希望它看起来像这样(可能):

文件名.txt -- 300 字节

filename2.txt -- 400 字节

filename3.txt -- 500 字节

等等。

到目前为止,我创建了一个可以打开文件的程序,它会打印字节,但它不会读取整个目录,我必须具体说明我要读取的文件..(即不是我想要的)。

这是我目前所拥有的:

#include <stdio.h>

int main(){
    FILE *fp; // file pointer
    long fileSize;
    int size;

    // opens specified file and reads
    fp = fopen( "importantcommands.txt", "rw" );
    
    if( fp == NULL ){
        printf( "Opening file error\n" );
        return 0;
    }

    // uses fileLength function and prints here
    size = fileLength(fp);
    printf( "\n Size of file: %d bytes", size );

    fclose(fp);

    return 0;
}

int fileLength( FILE *f ){
    int pos;
    int end;

    // seeks the beginning of the file to the end and counts
    // it and returns into variable end
    pos = ftell(f);
    fseek (f, 0, SEEK_END);
    end = ftell(f);
    fseek (f, pos, SEEK_SET);

    return end;
}

请帮忙。

【问题讨论】:

  • 刚刚编辑显示平台。 :) Windows 7!
  • 有用的错误信息很重要:fp = fopen( name, mode ); if( fp == NULL ) { perror( name ); }

标签: c file printing size names


【解决方案1】:

C 当然可以做到——例如,ls(1) 命令可以,而且它是用 C 编写的。

要遍历目录,您可以使用opendir(3)readdir(3) 函数。不过,让 shell 为你做这件事可能更容易。

就获取文件名而言,您可以将其作为命令行参数,将 main 定义为:

int main(int argc, char **argv)

命令行参数将从argv[1]开始。

【讨论】:

    【解决方案2】:

    如果您在dirent.h 中使用Linux,请参阅opendir() / fdopendir()readdir() man page

    来自SO Post的简单示例

    DIR *dir;  
    struct dirent *ent;
    if ((dir = opendir ("c:\\src\\")) != NULL) {
      /* print all the files and directories within directory */
      while ((ent = readdir (dir)) != NULL) {
         printf ("%s\n", ent->d_name);
      }
      closedir (dir);
    } 
    else {
      /* could not open directory */
      perror ("Could not open directory");
      return EXIT_FAILURE;
    }   
    

    您还可以使用fstat() 系统调用,它可以为您想要的任何文件填写struct stat。您可以从 stat 访问该文件的大小。
    请使用man 页面来帮助您。 (几乎)与 Linux 相关的所有内容都有非常详细的文档记录。

    【讨论】:

    • 好的,这个结果真的很棒。我能够列出目录的所有内容。现在有没有办法可以找到列出的每个文件的文件大小并在名称后打印?我已经这样做了(它只列出了第一个文件的大小):int main(){ DIR *dir; FILE *fp; struct dirent *ent; int size; if((dir = opendir("c:/")) != NULL){ while(fp = fopen("ex.txt", "rw")){ size = fileLength(fp); } while((ent = readdir (dir)) != NULL){ printf( "%s, size: %d\n", ent-&gt;d_name, size ); } closedir( dir ); } else{ perror( "" ); return -1; } }
    • fstat 统计一个已经打开的文件(它需要一个文件描述符);要统计尚未打开的文件,您应该改用stat(2)
    【解决方案3】:

    要读取目录中的文件列表,请查看opendirreaddir、closeir for Linux

    使用stat获取文件长度。

    这些是 Linux 的

    对于 winodws,请参阅 http://msdn.microsoft.com/en-gb/library/windows/desktop/aa365200%28v=vs.85%29.asp 和链接 http://blog.kowalczyk.info/article/8f/Get-file-size-under-windows.html 将向您展示如何执行此操作。

    【讨论】:

      【解决方案4】:

      要获取目录中的文件列表,请查找“libc opendir”。要在不打开文件的情况下获取文件的大小,可以使用 fstat。

      【讨论】:

        【解决方案5】:

        这似乎与我最近看到的另一个问题非常相似。无论如何,这是我奇怪的相似答案(对于 Linux,不确定它在 Windows 7 上的表现如何):

        #include <stdio.h>
        #include <dirent.h>
        #include <sys/stat.h>
        
        int main(int argc, char *argv[]) {
            struct stat file_stats;
            DIR *dirp;
            struct dirent* dent;
        
            dirp=opendir("."); // specify directory here: "." is the "current directory"
            do {
                dent = readdir(dirp);
                if (dent)
                {
                    printf("%s  --  ", dent->d_name);
                    if (!stat(dent->d_name, &file_stats))
                    {
                        printf("%u bytes\n", (unsigned int)file_stats.st_size);
                    }
                    else
                    {
                        printf("(stat() failed for this file)\n");
                    }
                }
            } while (dent);
            closedir(dirp);
        }
        

        【讨论】:

          【解决方案6】:

          对于给定的示例(在 Linux 或其他 UNIX 下),需要注意的事项很少。

          1. 您只想打印常规文件的文件名和大小。使用S_ISREG() 测试st_mode 字段
          2. 如果你还想递归打印出子目录下的所有文件,那么你需要使用S_ISDIR()来测试目录并注意特殊目录'.' and '..'

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-04-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-27
            • 2010-09-09
            • 1970-01-01
            • 2014-09-18
            相关资源
            最近更新 更多