读书人

stat得到的如何全是 directory

发布时间: 2012-06-12 14:21:25 作者: rapoo

stat得到的怎么全是 directory

C/C++ code
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <dirent.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <time.h>void print_stat_attr(const char *path){    DIR *dir;    struct dirent *ent;    struct stat buffer;    //char str;//    char buf;    dir = opendir(path);    stat(path, &buffer);    while ((ent = readdir(dir)) != NULL)    {        switch (buffer.st_mode & S_IFMT)        {        case S_IFBLK:            printf("block device");            break;        case S_IFCHR:            printf("character device");            break;        case S_IFDIR:            printf("directory");            break;        case S_IFIFO:            printf("FIFO/pipe");            break;        case S_IFLNK:            printf("symlink");            break;        case S_IFREG:            printf("regular file");            break;        case S_IFSOCK:            printf("socket");            break;        default:            printf("unknown?");            break;        }        //printf("%s", str);        printf(" %d", (int)buffer.st_ino);        printf(" %d", buffer.st_uid);        printf(" %d", buffer.st_gid);        printf(" %d", (int)buffer.st_size);        printf(" %d", (int)buffer.st_mode);        printf(" %s\n", ent->d_name);        //printf(" %s", (char *)ctime(&buffer.st_mtime));    }}int main(){    print_stat_attr("/home/lxd/workspace/android_4_0/.repo/manifests/.git");    return 0;}

在 这个目录下存放着这些文件:
drwxr-xr-x 2 lxd lxd 4096 6月 11 09:49 ./
drwxr-xr-x 3 lxd lxd 4096 2月 20 07:57 ../
lrwxrwxrwx 1 lxd lxd 26 2月 20 07:57 config -> ../../manifests.git/config
lrwxrwxrwx 1 lxd lxd 31 2月 20 07:57 description -> ../../manifests.git/description
lrwxrwxrwx 1 lxd lxd 24 2月 20 07:57 logs -> ../../manifests.git/logs/
lrwxrwxrwx 1 lxd lxd 24 2月 20 07:57 refs -> ../../manifests.git/refs/
lrwxrwxrwx 1 lxd lxd 28 2月 20 07:57 rr-cache -> ../../manifests.git/rr-cache/
-rw-rw-r-- 1 lxd lxd 0 6月 11 09:49 sdf
lrwxrwxrwx 1 lxd lxd 23 2月 20 07:57 svn -> ../../manifests.git/svn

程序运行的结果:
directory 662643 1000 1000 4096 16877 sdf
directory 662643 1000 1000 4096 16877 ..
directory 662643 1000 1000 4096 16877 .
directory 662643 1000 1000 4096 16877 svn
directory 662643 1000 1000 4096 16877 logs
directory 662643 1000 1000 4096 16877 description
directory 662643 1000 1000 4096 16877 config
directory 662643 1000 1000 4096 16877 rr-cache
directory 662643 1000 1000 4096 16877 refs


有没有人明白是怎么回事, 怎么全是directory呢




[解决办法]

lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.

读书人网 >C语言

热点推荐