2

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

助けてください。

4

6 に答える 6

5

Cは確かにそれを行うことができます-ls(1)たとえば、コマンドは可能で、Cで書かれています.

opendir(3)ディレクトリを反復処理するには、 関数と関数を使用できますreaddir(3)。ただし、シェルに任せた方がおそらく簡単です。

ファイル名を取得する限り、main を次のように定義することで、コマンド ライン パラメータとして使用できます。

int main(int argc, char **argv)

コマンド ライン パラメータは で始まりますargv[1]

于 2013-01-28T19:02:44.323 に答える
3

man ページで Linux を使用しているかどうかを参照opendir() / fdopendir()してください。readdir()dirent.h

からの簡単な例: SO ポスト

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 に関連するものはすべて、非常によく文書化されています。

于 2013-01-28T19:03:16.660 に答える
2

ディレクトリ内のファイルのリストを読み取るには、 Linux の場合はopendirreaddir、 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は、これを行う方法を示します。

于 2013-01-28T19:05:35.840 に答える
0

ディレクトリ内のファイルのリストを取得するには、「libc opendir」を探します。ファイルを開かずにファイルのサイズを取得するには、fstat を使用できます。

于 2013-01-28T19:03:12.460 に答える
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);
}
于 2013-01-30T22:20:31.197 に答える
0

与えられた例 (Linux または他の UNIX で) について注意する必要があることはほとんどありません。

  1. 通常のファイルのファイル名とサイズのみを適切に印刷したいだけです。フィールドS_ISREG()のテストに使用st_mode
  2. サブディレクトリの下にあるすべてのファイルも再帰的に出力したい場合は、使用S_ISDIR()してディレクトリをテストし、特別なディレクトリに注意する必要があります'.' and '..'
于 2013-01-30T23:40:45.900 に答える