• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            stat函數(shù)說明

            Posted on 2008-08-25 13:55 Prayer 閱讀(12646) 評論(1)  編輯 收藏 引用 所屬分類: LINUX/UNIX/AIX

            1 函數(shù)都是獲取文件(普通文件,目錄,管道,socket,字符,塊()的屬性。
            函數(shù)原型
            #include <sys/stat.h>

            int stat(const char *restrict pathname, struct stat *restrict buf);
            提供文件名字,獲取文件對應(yīng)屬性。
            int fstat(int filedes, struct stat *buf);
            通過文件描述符獲取文件對應(yīng)的屬性。
            int lstat(const char *restrict pathname, struct stat *restrict buf);
            連接文件描述命,獲取文件屬性。
            2 文件對應(yīng)的屬性
            struct stat {
                    mode_t     st_mode;       //文件對應(yīng)的模式,文件,目錄等
                    ino_t      st_ino;       //inode節(jié)點(diǎn)號
                    dev_t      st_dev;        //設(shè)備號碼
                    dev_t      st_rdev;       //特殊設(shè)備號碼
                    nlink_t    st_nlink;      //文件的連接數(shù)
                    uid_t      st_uid;        //文件所有者
                    gid_t      st_gid;        //文件所有者對應(yīng)的組
                    off_t      st_size;       //普通文件,對應(yīng)的文件字節(jié)數(shù)
                    time_t     st_atime;      //文件最后被訪問的時(shí)間
                    time_t     st_mtime;      //文件內(nèi)容最后被修改的時(shí)間
                    time_t     st_ctime;      //文件狀態(tài)改變時(shí)間
                    blksize_t st_blksize;    //文件內(nèi)容對應(yīng)的塊大小
                    blkcnt_t   st_blocks;     //偉建內(nèi)容對應(yīng)的塊數(shù)量
                  };
            可以通過上面提供的函數(shù),返回一個(gè)結(jié)構(gòu)體,保存著文件的信息。

             


             

            stat函數(shù)
            2008-07-26 20:25

            /* 最近要做簡單的linux文件管理的功能,用到函數(shù)相繼貼出來.很亂,待總結(jié)!*/

            /*reference :http://www.lslnet.com/linux/dosc1/10/linux-154990.htm*/
            /* dir.c */

            #include <sys/types.h>

            #include <dirent.h>

            #include <sys/stat.h>

            #include <string.h>

            #include <stdio.h>

            #include <stdlib.h>

            #include <unistd.h>



            extern int errno;

            /*

            DIR *opendir(const char *name);

            struct dirent *readdir(DIR *dir);

            int closedir(DIR *dir);

            */



            int main(int argc,char *argv[])

            {

            DIR *dir;

            struct dirent *pdir;

            struct stat st;

            char buf[1024];

            char c_type[6][9]={"普通文件","目錄","連接","\0","其他格式","\0"};

            int type;

            int n;



            if(argc>2)

            {

            printf("Usage: dir /path\n");

            return 0;

            }

            if(argc==2)

            strncpy(buf,argv[1],99);

            else

            strcpy(buf,".");



            printf("%s\n\n",buf);

            dir=opendir(buf);

            if(dir==NULL)

            {

            printf("Can not open %s\n",buf);

            return 0;

            }

            n = strlen(buf);

            if (buf[n-1] != '/') {

            strcat(buf, "/");

            n++;

            }

            while((pdir=readdir(dir))!=NULL)

            {

            strcat(buf, pdir->d_name);

            if(stat(buf,&st)==0)

            {

            if(st.st_mode&S_IFREG)

            type=0;

            else if(st.st_mode&S_IFDIR)

            type=1;

            else if(st.st_mode&S_IFLNK)

            type=2;

            else type=4;

            printf("%-10s%-20s%7d\n",c_type[type],pdir->d_name,(int)st.st_size);

            }

            else

            printf("%-20s%-10d%-10d\n",pdir->d_name,st.st_mode,errno);

            bzero(&st,sizeof(struct stat));

            buf[n]='\0';

            }

            closedir(dir);

            return 0;

            Feedback

            # re: stat函數(shù)說明  回復(fù)  更多評論   

            2008-12-23 09:25 by EiN
            不知博主有無使用stat時(shí)奇怪的現(xiàn)象。有時(shí)可以獲得文件的大小,有時(shí)返回錯(cuò)誤。實(shí)在不知為何。
            日本三级久久网| 久久久一本精品99久久精品66 | 91久久九九无码成人网站| 国产99久久久久久免费看| 伊人久久大香线蕉AV一区二区| 亚洲色大成网站www久久九| 欧美777精品久久久久网| 亚洲国产成人乱码精品女人久久久不卡 | 久久影院综合精品| 国产伊人久久| 精品久久久久久久| 亚洲精品乱码久久久久久中文字幕| 国产国产成人久久精品| 欧美大香线蕉线伊人久久| 精品无码人妻久久久久久| 精品国产乱码久久久久久1区2区| 欧美久久久久久精选9999| 久久综合狠狠色综合伊人| 婷婷综合久久中文字幕蜜桃三电影| 国产精品久久久久久久久久免费| 久久精品99久久香蕉国产色戒 | 日韩精品久久久久久久电影| 国产精久久一区二区三区| 久久99国产亚洲高清观看首页| 久久精品人人做人人爽电影| 亚洲欧美成人久久综合中文网| 国产无套内射久久久国产| 久久99精品久久久久久| 97精品国产91久久久久久| 97久久精品人妻人人搡人人玩| 日韩人妻无码精品久久久不卡| 无码日韩人妻精品久久蜜桃 | 久久777国产线看观看精品| 久久久久女人精品毛片| 精品熟女少妇a∨免费久久| AV狠狠色丁香婷婷综合久久| 久久精品夜夜夜夜夜久久| 97久久精品无码一区二区天美| 久久国产免费观看精品3| 久久99免费视频| 久久综合视频网站|