• <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>
            posts - 319, comments - 22, trackbacks - 0, articles - 11
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            找不到 dirent.h 文件

            Posted on 2012-04-28 06:42 RTY 閱讀(9701) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C/C++ 、Windows
            轉(zhuǎn)載:http://www.cnblogs.com/fzzl/archive/2009/07/14/1522913.html

            【轉(zhuǎn)】vs2005下的dirent.h  該方法同樣適用于VS2008 及VS2010

            http://www.analogcn.com/Article/wz3/200802/20080202211037.html

             

            dirent.h是gcc下的一個(gè)頭文件,而在VS2005中是沒(méi)有的。這個(gè)文件中封裝了幾個(gè)對(duì)目錄進(jìn)行操作函數(shù):

            static DIR *opendir (const char *dirname);
            static struct dirent *readdir (DIR *dirp);
            static int closedir (DIR *dirp);

             對(duì)于在linux->windows之間進(jìn)行程序移植來(lái)講常常會(huì)造成一些困擾,在網(wǎng)上仔細(xì)搜了一下,發(fā)現(xiàn)原來(lái)已經(jīng)有位好同志寫(xiě)了相應(yīng)的移植代碼,如下所示:


            typedef struct dirent {
              /* name of current directory entry (a multi-byte character string) */
              char d_name[MAX_PATH + 1];

              /* file attributes */
              WIN32_FIND_DATA data;
            } dirent;


            typedef struct DIR {
              /* current directory entry */
              dirent current;

              /* is there an un-processed entry in current? */
              int cached;

              /* file search handle */
              HANDLE search_handle;

              /* search pattern (3 = zero terminator + pattern "\\*") */
              TCHAR patt[MAX_PATH + 3];
            } DIR;


            static DIR *opendir (const char *dirname);
            static struct dirent *readdir (DIR *dirp);
            static int closedir (DIR *dirp);


            /* use the new safe string functions introduced in Visual Studio 2005 */
            #if defined(_MSC_VER) && _MSC_VER >= 1400
            # define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
            #else
            # define STRNCPY(dest,src,size) strncpy((dest),(src),(size))
            #endif


            /*
             * Open directory stream DIRNAME for read and return a pointer to the
             * internal working area that is used to retrieve individual directory
             * entries.
             */
            static DIR*
            opendir(
                const char *dirname)
            {
              DIR *dirp;
              assert (dirname != NULL);
              assert (strlen (dirname) < MAX_PATH);

              /* construct new DIR structure */
              dirp = (DIR*) malloc (sizeof (struct DIR));
              if (dirp != NULL) {
                TCHAR *p;
               
                /* prepare search pattern */
            #ifdef _UNICODE

                /* convert directory name to wide character string */
                MultiByteToWideChar(
                    CP_ACP,                                /* code page */
                    0,                                     /* conversion flags */
                    dirname,                               /* mb-string to convert */
                    -1,                                    /* length of mb-string */
                    dirp->patt,                            /* wc-string to produce */
                    MAX_PATH);                             /* max length of wc-string */
                dirp->patt[MAX_PATH] = '\0';
               
                /* append search pattern to directory name */
                p = wcschr (dirp->patt, '\0');
                if (dirp->patt < p  &&  *(p-1) != '\\'  &&  *(p-1) != ':') {
                  *p++ = '\\';
                }
                *p++ = '*';
                *p = '\0';

            #else /* !_UNICODE */
               
                /* take directory name... */
                STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
                dirp->patt[MAX_PATH] = '\0';
               
                /* ... and append search pattern to it */
                p = strchr (dirp->patt, '\0');
                if (dirp->patt < p  &&  *(p-1) != '\\'  &&  *(p-1) != ':') {
                  *p++ = '\\';
                }
                *p++ = '*';
                *p = '\0';
               
            #endif /* !_UNICODE */

                /* open stream and retrieve first file */
                dirp->search_handle = FindFirstFile (dirp->patt, &dirp->current.data);
                if (dirp->search_handle == INVALID_HANDLE_VALUE) {
                  /* invalid search pattern? */
                  free (dirp);
                  return NULL;
                }

                /* there is an un-processed directory entry in memory now */
                dirp->cached = 1;
               
              }
              return dirp;
            }


            /*
             * Read a directory entry, and return a pointer to a dirent structure
             * containing the name of the entry in d_name field.  Individual directory
             * entries returned by this very function include regular files,
             * sub-directories, pseudo-directories "." and "..", but also volume labels,
             * hidden files and system files may be returned. 
             */
            static struct dirent *
            readdir(
                DIR *dirp)
            {
              assert (dirp != NULL);

              if (dirp->search_handle == INVALID_HANDLE_VALUE) {
                /* directory stream was opened/rewound incorrectly or it ended normally */
                return NULL;
              }

              /* get next directory entry */
              if (dirp->cached != 0) {
                /* a valid directory entry already in memory */
                dirp->cached = 0;
              } else {
                /* read next directory entry from disk */
                if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {
                  /* the very last file has been processed or an error occured */
                  FindClose (dirp->search_handle);
                  dirp->search_handle = INVALID_HANDLE_VALUE;
                  return NULL;
                }
              }

              /* copy directory entry to d_name */
            #ifdef _UNICODE
             
              /* convert entry name to multibyte */
              WideCharToMultiByte(
                  CP_ACP,                                  /* code page */
                  0,                                       /* conversion flags */
                  dirp->current.data.cFileName,            /* wc-string to convert */
                  -1,                                      /* length of wc-string */
                  dirp->current.d_name,                    /* mb-string to produce */
                  MAX_PATH,                                /* max length of mb-string */
                  NULL,                                    /* use sys default character */
                  NULL);                                   /* don't care  */
              dirp->current.d_name[MAX_PATH] = '\0';
             
            #else /* !_UNICODE */

              /* copy as a multibyte character string */
              STRNCPY (dirp->current.d_name, dirp->current.data.cFileName, sizeof(dirp->current.d_name));
              dirp->current.d_name[MAX_PATH] = '\0';

            #endif /* !_UNICODE */
             
              return &dirp->current;
            }


            /*
             * Close directory stream opened by opendir() function.  Close of the
             * directory stream invalidates the DIR structure as well as any previously
             * read directory entry.
             */
            static int
            closedir(
                DIR *dirp)
            {
              assert (dirp != NULL);
             
              /* release search handle */
              if (dirp->search_handle != INVALID_HANDLE_VALUE) {
                FindClose (dirp->search_handle);
                dirp->search_handle = INVALID_HANDLE_VALUE;
              }

              /* release directory handle */
              free (dirp);
              return 0;
            }

            此文件可從http://www.softagalleria.net/dirent/index.en.html下載得到,直接將它放在VS2005的include目錄就OK 了!

            開(kāi)網(wǎng)店http://www.5678520.com/怎么樣開(kāi)網(wǎng)店

            婷婷久久综合| 久久人人爽人人爽人人片AV不| 成人久久久观看免费毛片| 人人狠狠综合久久亚洲婷婷| 国产叼嘿久久精品久久| 久久精品免费全国观看国产| 日韩av无码久久精品免费| 蜜臀久久99精品久久久久久小说 | 久久久久久亚洲Av无码精品专口| 国产成人精品久久免费动漫 | 精品久久国产一区二区三区香蕉| 亚洲AV无码久久| 狠狠综合久久综合88亚洲 | 欧美色综合久久久久久| 久久久久这里只有精品| 国产69精品久久久久99尤物| 色综合久久精品中文字幕首页| 亚洲AV无码久久精品狠狠爱浪潮| 欧美激情精品久久久久久| 久久99精品久久久久久9蜜桃| 久久99国产精品一区二区| 国产精品99久久久久久人| 国产精品久久久久天天影视| 美女写真久久影院| 伊人色综合九久久天天蜜桃 | 国产精品热久久毛片| 亚洲精品国精品久久99热| 99久久精品免费看国产一区二区三区 | 国产成人久久激情91| 99久久人人爽亚洲精品美女| 老司机午夜网站国内精品久久久久久久久| 久久婷婷五月综合97色直播| 精品伊人久久久| 国产精品青草久久久久福利99| 久久精品国产欧美日韩99热| 久久精品草草草| 亚洲乱码精品久久久久.. | 一本久久a久久精品vr综合| 久久99精品国产麻豆蜜芽| av无码久久久久不卡免费网站| 久久亚洲中文字幕精品一区|