Unix to Windows Porting Dictionary for HPC |
||
|
|
||
|
|
LinksFunction List
|
Table of Contents header files: sys/types.h, dirent.h DIR * opendir (const char *filename); DIR * wcs_opendir (const wchar_t *filename); struct dirent * readdir (DIR *dirp); int readdir_r (DIR *dirp, struct dirent *entry, struct dirent **result); void rewinddir (DIR *dirp); int closedir (DIR *dirp); long telldir (DIR *dirp); void seekdir (DIR *dirp, long loc); int dirfd (DIR *dirp); header file: Winbase.h, Windows.h HANDLE WINAPI FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData); HANDLE WINAPI FindFirstFileEx(LPCTSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags); BOOL WINAPI FindNextFile(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData); BOOL WINAPI FindClose(HANDLE hFindFile); The core of these Unix functions is to read the names of the files and sub-directories in a directory. The other functions are for determining and setting the read position. Direct equivalent functions are not available as Windows functions, but logically close functions are available for reading directory entries. There are no matching functions for repositioning the read pointer (seekdir) or getting the read position (telldir). There is no method for obtaining the "raw" file handle (descriptor) to the directory. To begin the first read of the directory either FindFirstFile() or FindFirstFileEx() must be called. These two functions both open the directory and read the first entry. Where the Unix readdir() function reads all directory entries, the two windows functions has filtering capabilities. FindFirstFile() allows for basic filter capabilities and FindFirstFileEx allows for more complex filter capabilities. Subsequent reads of the directory are done using FindNextFile which continues with the same filter as the initial read. When the last entry is read, or the last entry you are interested in is read, then FindClose will release all resources. You may have several directory reads happening at the same time. The FindFirstFile() using a filter specification of "*" (wildcard) will match the behavior on Unix systems. If your Unix program originally filtered the information returned from readdir() you can continue to use it, or you may integrate it to the FindFirstFile() or FindFirstFileEx() functions for efficiency. To perform a rewind of the directory pointer (rewinddir) you must close the handle and start a new FindFirstFile. The principal Windows filesystems, NTFS, allows for one or more streams to be associated with each file or directory. This is usually not an issue when porting source code from Unix. However, being aware of this capability may be useful in the future. The reference functions are FindFirstStreamW() and FindNextStreamW()
HANDLE hFind;
WIN32_FIND_DATA FData;
if ((hFind = FindFirstFile("C:/tmp/*", &FData)) != INVALID_HANDLE_VALUE) {
do{
/* have a directory entry to use */
} while (FindNextFile(hFind, &FData));
FindClose(hFind);
}
|