遍历文件夹

POSIX Directory Browsing API for Windows

The functions and types specified in POSIX for iterating over directory entries have been defined here as wrappers for porting to and common use on Windows platforms. The values of  errno  set in the event of errors are the most significant difference between the POSIX definition and the wrapper API.

In addition to this documentation file, the software is provided in the dirent.h header file and the dirent.c C source file. To use the API ensure that the path to the dirent.h header is either somewhere standard or is provided to the compiler as an additional option. Ensure also that the dirent.c file is compiled and the object file is either referenced explicitly in the link or included in a referenced library. The source code also compiles cleanly as C++, but it retains C linkage.


<dirent.h>

  • typedef ... DIR;
    
    struct dirent
    {
        char *d_name;
    };
    
    DIR           *opendir(const char *name);
    int            closedir(DIR *dir);
    struct dirent *readdir(DIR *dir);
    void           rewinddir(DIR *dir);
    

DIR *opendir(const char *name);

Description
  • The  opendir function opens the directory specified by  name, which may use either  / or  \ as a directory separator but should not contain any wildcards. On success it associates a  DIR stream with the open directory. A non-null  DIR stream may be used in subsequent calls to  readdir, rewinddir and  closedir.

    A successful result will position the DIR stream at the first directory entry, ready for reading. Note that a truly empty directory (one without even . or .. entries) will not open successfully.

Returns
  • A pointer to the  DIR structure for the opened directory on success, otherwise null on failure.
Errors
  • ENOENT   No such directory.
    EINVAL   Invalid argument or directory name.
    ENOMEM   Not enough memory to perform the operation.

int closedir(DIR *dir);

Description
  • The  closedir function closes the directory stream associated with  dir, freeing resources as necessary and invalidating the  dir pointer.
Returns
  • Returns  0 on successful completion, otherwise  -1.
Errors
  • EBADF    Invalid directory stream.

struct dirent *readdir(DIR *dir);

Description
  • The  readdir function is used to iterate through the directory stream  dir. It advances it one entry at a time, details of which it returns as its result.

    On NTFS and FAT file systems, except for drive root directories, the caller is guaranteed that the . and .. entries will be included in the directory stream. On FATX file systems the . and .. entries are not included.

Returns
  • Returns a pointer to the directory details on success, in which  d_name is the file name of the current entry, otherwise null on error or end of stream.
Errors
  • ENOENT   No more entries.
    EBADF    Invalid directory stream.

void rewinddir(DIR *dir);

Description
  • The  rewindir function can be used to reset the directory stream  dir to the start. Sensible results cannot be guaranteed if the directory name used in the initial call to  opendir was a relative path name and the program has since changed its current working directory.
Returns
  • No error status is returned.
Errors
  • EBADF    Invalid directory stream.


For Unix/Linux based systems:

You can use opendir / readdir / closedir.

Sample code which searches a directory for entry ``name'' is:

   len = strlen(name);
   dirp = opendir(".");
   while ((dp = readdir(dirp)) != NULL)
           if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
                   (void)closedir(dirp);
                   return FOUND;
           }
   (void)closedir(dirp);
   return NOT_FOUND;

Source code from the above man pages.


For a windows based systems:

you can use the Win32 API FindFirstFile / FindNextFile / FindClose functions.

The following C++ example shows you a minimal use of FindFirstFile.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

void _tmain(int argc, TCHAR *argv[])
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind;

   if( argc != 2 )
   {
      _tprintf(TEXT("Usage: %s [target_file]\n"), argv[0]);
      return;
   }

   _tprintf (TEXT("Target file is %s\n"), argv[1]);
   hFind = FindFirstFile(argv[1], &FindFileData);
   if (hFind == INVALID_HANDLE_VALUE) 
   {
      printf ("FindFirstFile failed (%d)\n", GetLastError());
      return;
   } 
   else 
   {
      _tprintf (TEXT("The first file found is %s\n"), 
                FindFileData.cFileName);
      FindClose(hFind);
   }
}

Source code from the above msdn pages.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值