1

Cのstat関数に問題があります。アプリケーションは2つのディレクトリ(2番目のディレクトリはまだ実装されていません)にあるすべてのファイルを一覧表示する必要があります。dir1が「。」に設定されている場合。現在のディレクトリの場合は、すべてのファイルが一覧表示されます。必要なディレクトリに変更すると、1つのファイルしかリストされません。

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

main ()
{
    DIR *dir1;
    DIR *dir2;
    dir1 = opendir ("/home/tom/Documents/Uni/Dropbox/OS/C/1/");
    dir2 = opendir ("/home/tom/Documents/Uni/Dropbox/OS/C/2/");
    struct dirent *ent;
    struct stat fileStat;

    if (dir1 != NULL) 
    {
        /* while there are files to read in the directory */
        while ((ent = readdir (dir1)) != NULL) 
        {
        /*printf ("In 1\n"); <--debugging--> */
        if (stat(ent->d_name,&fileStat) == 0)
        {
            /* ignores . and .. and hidden files */
            /* printf ("In 2\n"); <--debugging--> */
            if(ent->d_name[0] != '.')
            {
                /* printf ("In 3\n"); <--debugging--> */
                printf ("\n");
                printf ("File: %s\n", ent->d_name);
                printf ("File size: %d\n", fileStat.st_size);
                printf ("-----------------------------------\n");
            }
        }
    }
    /* close the 1st directory */
    closedir (dir1);
    /* close the 2nd directory */
    closedir (dir2);
    }
    else 
    {
        /* prints an error if the  directory can not be opened */
        perror ("");
    } 
}

プログラムを実行した結果は次のとおりです。

tom@x60deb:~/Documents/Uni/Dropbox/OS/C$ ./ffffuuuuuu 

File: ffffuuuuuu.c
File size: 1045
-----------------------------------

これは、読み取っているディレクトリ内のlsの結果です。

tom@x60deb:~/Documents/Uni/Dropbox/OS/C/1$ ls -l
total 36
-rw-r--r-- 1 tom tom 356 Dec 12 23:36 cwTest2.c
-rw-r--r-- 1 tom tom 322 Dec 12 23:36 cwTest.c
-rw-r--r-- 1 tom tom 627 Dec 12 23:36 ffffuuuuuu.c
-rw-r--r-- 1 tom tom   6 Dec 12 23:32 file
-rw-r--r-- 1 tom tom   6 Dec 12 23:32 file2
-rw-r--r-- 1 tom tom   6 Dec 12 23:45 file2.file
-rw-r--r-- 1 tom tom  15 Dec 12 23:33 file3
-rw-r--r-- 1 tom tom  15 Dec 12 23:45 file3.file
-rw-r--r-- 1 tom tom   6 Dec 12 23:45 file.file

トム、よろしくお願いします。

4

2 に答える 2

1

stat()名前は、絶対パス名または現在のディレクトリからの相対名のいずれかとして指定する必要があります。

スキャナーがディレクトリを変更している場合、(a)あなたは私よりも勇敢であり、(b)短い名前を使用できますが、(c)最初の場所に戻る方法について心配する必要があります。

POSIX 2008を使用している場合*at()は、システムコールのバリアントを使用して作業を簡素化できる場合があります。しかし、これらの呼び出しをサポートしているシステムが(あるとしても)いくつあるかはまだわかりません。

于 2011-12-13T00:26:07.297 に答える
0
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

int main (void)
{
    char * dirname = "/home/tom/Documents/Uni/Dropbox/OS/C/1/" ;
    DIR *dir1;
    char path[11111];
    size_t len;
    struct dirent *ent;
    struct stat fileStat;

    dir1 = opendir (dirname);
    len = strlen ( dirname);
    memcpy(path, dirname, len+1);

    struct dirent *ent;
    struct stat fileStat;

    if (dir1 != NULL) 
    {
        /* while there are files to read in the directory */
        while ((ent = readdir (dir1)) != NULL) 
        {
        /*printf ("In 1\n"); <--debugging--> */
        strcpy(path+len, ent->d_name);
        if (stat( path,&fileStat) == 0)
        {
            /* ignores . and .. and hidden files */
            /* printf ("In 2\n"); <--debugging--> */
            if(ent->d_name[0] != '.')
            {
                /* printf ("In 3\n"); <--debugging--> */
                printf ("\n");
                printf ("File: %s\n", ent->d_name);
                printf ("File size: %d\n", fileStat.st_size);
                printf ("-----------------------------------\n");
            }
        }
    }
    /* close the 1st directory */
    closedir (dir1);
    }
    else 
    {
        /* prints an error if the  directory can not be opened */
        perror ("");
    }
return 0; 
}

一部の人が読めないので更新してください、私はここに私の元のコメントを追加します:

現在のディレクトリは何ですか?エントリは「/home/ tom / Documents / Uni / Dropbox / OS / C / 1 /」に関連しています(stat()にフルパス名を指定する必要があります)また、「。」よりも多くのエントリがあります。および「。」で始まる「..」

于 2011-12-13T00:13:11.667 に答える