• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            stat函數說明

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

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

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

             


             

            stat函數
            2008-07-26 20:25

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

            /*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函數說明  回復  更多評論   

            2008-12-23 09:25 by EiN
            不知博主有無使用stat時奇怪的現象。有時可以獲得文件的大小,有時返回錯誤。實在不知為何。
            午夜精品久久久久久中宇| 久久国产精品成人免费 | 天天综合久久一二三区| 久久露脸国产精品| 亚洲国产精品无码久久久不卡 | 中文字幕无码久久精品青草| 精品国产乱码久久久久软件| 国内精品久久久久影院日本| 青青草原综合久久大伊人导航 | 99re这里只有精品热久久| 国产精品亚洲美女久久久| 亚洲精品无码久久一线| 久久午夜福利电影| 99久久免费国产精品热| 狠狠综合久久综合88亚洲| 99久久99久久| 亚洲午夜久久久久妓女影院| 久久国产香蕉一区精品| 国产精品久久精品| 久久综合九色综合网站| 少妇被又大又粗又爽毛片久久黑人 | 久久久久久久久久免免费精品| 伊人久久综合无码成人网| 欧美一级久久久久久久大片| 亚洲国产精品热久久| 成人久久免费网站| 日产久久强奸免费的看| 久久久久久青草大香综合精品| 99久久精品日本一区二区免费| 久久婷婷国产剧情内射白浆 | 韩国免费A级毛片久久| 亚洲色大成网站www久久九| 日本国产精品久久| 一97日本道伊人久久综合影院| 久久久99精品成人片中文字幕| 999久久久国产精品| segui久久国产精品| 久久这里只精品国产99热| 久久亚洲国产精品一区二区| 亚洲一区二区三区日本久久九| 曰曰摸天天摸人人看久久久|