用C打印文件名及其'大小

时间:2013-01-28 18:58:46

标签: c file printing size names

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

  

filename.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;
}

请帮忙。

6 个答案:

答案 0 :(得分:5)

C当然可以这样做 - 例如,ls(1)命令可以用C语言编写。

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

就获取文件名而言,您可以通过将main定义为:

将其作为命令行参数
int main(int argc, char **argv)

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

答案 1 :(得分:3)

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

来自a: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相关的所有内容都有很好的文档记录。

答案 2 :(得分:2)

要阅读目录中的文件列表,请查看opendirreaddir,Linux的closedir

使用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将告诉您如何执行此操作。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这似乎与我最近看到的另一个问题非常相似。无论如何,这是我奇怪的类似答案(对于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);
}

答案 5 :(得分:0)

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

  1. 您只是想要打印出常规文件的文件名和大小。使用S_ISREG()来测试st_mode字段
  2. 如果您想以递归方式打印出子目录下的所有文件,则需要使用S_ISDIR()来测试direcotry并注意特殊目录'.' and '..'
相关问题