LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1103|回复: 6

新手求教:为什么这个stat函数不工作?

[复制链接]
发表于 2009-7-29 17:33:23 | 显示全部楼层 |阅读模式
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>

int main(void)
{
    DIR *dp;
    struct dirent *ep;
    struct stat st;
    char dirp[50];

    printf("please input a dir name \n");
    scanf("%s",dirp);

    dp=opendir(dirp);

    printf("filename:\ttype:\tpermission\taccesstime\tlastmodtime\tsize\t\n");

    if(dp!=NULL)
    {
        while((ep = readdir(dp))!=NULL)
        {
                printf("%s\n",ep->d_name);
            if(stat(ep->d_name,&st)>=0)
            {
//                printf("%s\t",ep->d_name);
                if((st.st_mode&S_IFMT)==S_IFDIR)
                    printf("directory\t");
                else if((st.st_mode&S_IFMT)==S_IFBLK)
                    printf("block file\t");
                else if((st.st_mode&S_IFMT)==S_IFCHR)
                    printf("character file\t");
                else if((st.st_mode&S_IFMT)==S_IFREG)
                    printf("ordinary file\t");
                else if((st.st_mode&S_IFMT)==S_IFIFO)
                    printf("pipefile\t");
                else
                    printf("other\t");
                printf("%o\t",st.st_mode&0xfff);
                printf("%15s\t",ctime(&st.st_atime));
                printf("%15s\t",ctime(&st.st_mtime));
                printf("%ld\n",st.st_size);
            }else
                printf("stat failed\n");
        }
    }else
        puts("could not open the dir \n");

    closedir(dp);

    return 0;
}




是哪里错了 ,总是打印 stat failed 这一句。
 楼主| 发表于 2009-7-29 20:28:43 | 显示全部楼层
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>

int main(void)
{
    DIR *dp;
    struct dirent *ep;
    struct stat st;
    char dirp[50];

    printf("please input a dir name \n");
    scanf("%s",dirp);

    dp=opendir(dirp);

    printf("filename:\ttype:\tpermission\taccesstime\tlastmodtime\tsize\t\n");

    if(dp!=NULL)
    {
        while((ep = readdir(dp))!=NULL)
        {
                printf("%s\n",ep->d_name);
            if(stat(ep->d_name,&st)>=0)
            {
//                printf("%s\t",ep->d_name);
                if((st.st_mode&S_IFMT)==S_IFDIR)
                    printf("directory\t");
                else if((st.st_mode&S_IFMT)==S_IFBLK)
                    printf("block file\t");
                else if((st.st_mode&S_IFMT)==S_IFCHR)
                    printf("character file\t");
                else if((st.st_mode&S_IFMT)==S_IFREG)
                    printf("ordinary file\t");
                else if((st.st_mode&S_IFMT)==S_IFIFO)
                    printf("pipefile\t");
                else
                    printf("other\t");
                printf("%o\t",st.st_mode&0xfff);
                printf("%15s\t",ctime(&st.st_atime));
                printf("%15s\t",ctime(&st.st_mtime));
                printf("%ld\n",st.st_size);
            }else
                printf("stat failed\n");
        }
    }else
        puts("could not open the dir \n");

    closedir(dp);

    return 0;
}




是哪里错了 ,总是打印 stat failed 这一句。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-7-29 20:29:36 | 显示全部楼层
明明有空格,怎么不显示?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-7-30 09:06:58 | 显示全部楼层
为什么stat函数对有的目录工作,有的目录不工作呢?
回复 支持 反对

使用道具 举报

发表于 2009-8-4 11:37:54 | 显示全部楼层
在opendir之后,chdir一下,就可以了:

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

int main(void)
{
        DIR *dp;
        struct dirent *ep;
        struct stat st;
        char dirp[50];

        printf("please input a dir name:\n");
        scanf("%s", dirp);

        printf("filename:\ttype:\tpermission\taccesstime\tlastmodtime\tsize\t\n");
        
        dp = opendir(dirp);
        chdir(dirp);
        if (dp != NULL) {
                while ((ep = readdir(dp)) != NULL) {
                        if (strcmp(".", ep->d_name) ==0 ||
                                strcmp("..", ep->d_name) ==0 ) {
                                continue;
                        }
                        printf("%s\n", ep->d_name);
                        if (stat(ep->d_name, &st) >= 0) {
                                // printf("%s\t",ep->d_name);
                                if ((st.st_mode&S_IFMT)==S_IFDIR)
                                        printf("directory\t");
                                else if ((st.st_mode&S_IFMT)==S_IFBLK)
                                        printf("block file\t");
                                else if ((st.st_mode&S_IFMT)==S_IFCHR)
                                        printf("character file\t");
                                else if ((st.st_mode&S_IFMT)==S_IFREG)
                                        printf("ordinary file\t");
                                else if ((st.st_mode&S_IFMT)==S_IFIFO)
                                        printf("pipefile\t");
                                else
                                        printf("other\t");
                                printf("%o\t", st.st_mode&0xfff);
                                printf("%15s\t", ctime(&st.st_atime));
                                printf("%15s\t", ctime(&st.st_mtime));
                                printf("%ld\n", st.st_size);
                        } else {
                                printf("stat failed\n");
                        }
                }
        } else {
                puts("could not open the dir \n");
        }

        closedir(dp);

        return 0;
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-8-5 15:32:34 | 显示全部楼层
感谢指点 !
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-8-6 20:48:37 | 显示全部楼层
感谢楼上的指点 !
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表