by NoWest » 2005年11月05日(土) 14:03
> C(UNIX系のみ),およびPerl(Win/UNIXともに)に存在する「opendir」「readdir」等に相当することをABで(=API等を用いて)することは可能なのでしょうか。
密かにWindows版のVC++でもopendirは使えます。
(かなり使い方は異なっていますが… 感覚的にはfopenに近い)
> ただし,FindFirstFile/FindNextFileを使うものを除きます(その方法なら私も分かります)。
fopenはcrtdllに入っているのでopendirも入っているかと調べてみても
dllにはopendirは含まれていないようなので、コンパイルされたEXEを探ったところVC++のopendirはVC++のコンパイラがFind~File系のAPIに置き換えているようです。ということでopendirに相当するAPIは無いっぽいです。
**追加情報 興味のある人だけどうぞ。** [ここをクリックすると内容が表示されます] [ここをクリックすると非表示にします]
MSDNにUNIXからの移植サンプルがありました。
コード: 全て選択
UNIX example 1: using directory handling functions
This sample prints out the current directory, and then recurses through subdirectories.
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void ScanDir(char *dir, int indent)
{
DIR *dp;
struct dirent *dir_entry;
struct stat stat_info;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((dir_entry = readdir(dp)) != NULL) {
lstat(dir_entry->d_name,&stat_info);
if(S_ISDIR(stat_info.st_mode)) {
/* Directory, but ignore . and .. */
if(strcmp(".",dir_entry->d_name) == 0 ||
strcmp("..",dir_entry->d_name) == 0)
continue;
printf("%*s%s/\n",indent,"",dir_entry->d_name);
/* Recurse at a new indent level */
ScanDir(dir_entry->d_name,indent+4);
}
else printf("%*s%s\n",indent,"",dir_entry->d_name);
}
chdir("..");
closedir(dp);
}
int main(int argc, char* argv[])
{
char *topdir, defaultdir[2]=".";
if (argc != 2) {
printf("Argument not supplied - using current
directory.\n");
topdir=defaultdir;
}
else
topdir=argv[1];
printf("Directory scan of %s\n",topdir);
ScanDir(topdir,0);
printf("done.\n");
exit(0);
}
コード: 全て選択
Win32 example 1: using directory handling functions
This sample prints out the current directory, and then recurses through subdirectories. It uses the FindFirstFile(), FindNextFile(), and FindClose() Win32 API functions.
#include <windows.h>
#include <stdio.h>
void ScanDir(char *dirname, int indent)
{
BOOL fFinished;
HANDLE hList;
TCHAR szDir[MAX_PATH+1];
TCHAR szSubDir[MAX_PATH+1];
WIN32_FIND_DATA FileData;
// Get the proper directory path
sprintf(szDir, "%s\\*", dirname);
// Get the first file
hList = FindFirstFile(szDir, &FileData);
if (hList == INVALID_HANDLE_VALUE)
{
printf("No files found\n\n");
}
else
{
// Traverse through the directory structure
fFinished = FALSE;
while (!fFinished)
{
// Check the object is a directory or not
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((strcmp(FileData.cFileName, ".") != 0) &&
(strcmp(FileData.cFileName, "..") != 0))
{
printf("%*s%s\\\n", indent, "",
FileData.cFileName);
// Get the full path for sub directory
sprintf(szSubDir, "%s\\%s", dirname,
FileData.cFileName);
ScanDir(szSubDir, indent + 4);
}
}
else
printf("%*s%s\n", indent, "", FileData.cFileName);
if (!FindNextFile(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
}
}
}
FindClose(hList);
}
void main(int argc, char *argv[])
{
char *pszInputPath;
char pwd[2] = ".";
if (argc < 2)
{
printf("Argument not supplied - using current directory.\n");
pszInputPath = pwd;
}
else
{
pszInputPath = argv[1];
printf("Input Path: %s\n\n", pszInputPath);
}
ScanDir(pszInputPath, 0);
printf("\ndone.\n");
}
これを見る通りopendirの移植にはFind~File系のAPIを推奨しているようです。
[quote]> C(UNIX系のみ),およびPerl(Win/UNIXともに)に存在する「opendir」「readdir」等に相当することをABで(=API等を用いて)することは可能なのでしょうか。[/quote]
密かにWindows版のVC++でもopendirは使えます。
(かなり使い方は異なっていますが… 感覚的にはfopenに近い)
[quote]> ただし,[u]FindFirstFile/FindNextFileを使うものを除きます[/u](その方法なら私も分かります)。[/quote]
fopenはcrtdllに入っているのでopendirも入っているかと調べてみても
dllにはopendirは含まれていないようなので、コンパイルされたEXEを探ったところVC++のopendirはVC++のコンパイラがFind~File系のAPIに置き換えているようです。ということでopendirに相当するAPIは無いっぽいです。
[hide=**追加情報 興味のある人だけどうぞ。**]
MSDNにUNIXからの移植サンプルがありました。
[code]UNIX example 1: using directory handling functions
This sample prints out the current directory, and then recurses through subdirectories.
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void ScanDir(char *dir, int indent)
{
DIR *dp;
struct dirent *dir_entry;
struct stat stat_info;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((dir_entry = readdir(dp)) != NULL) {
lstat(dir_entry->d_name,&stat_info);
if(S_ISDIR(stat_info.st_mode)) {
/* Directory, but ignore . and .. */
if(strcmp(".",dir_entry->d_name) == 0 ||
strcmp("..",dir_entry->d_name) == 0)
continue;
printf("%*s%s/\n",indent,"",dir_entry->d_name);
/* Recurse at a new indent level */
ScanDir(dir_entry->d_name,indent+4);
}
else printf("%*s%s\n",indent,"",dir_entry->d_name);
}
chdir("..");
closedir(dp);
}
int main(int argc, char* argv[])
{
char *topdir, defaultdir[2]=".";
if (argc != 2) {
printf("Argument not supplied - using current
directory.\n");
topdir=defaultdir;
}
else
topdir=argv[1];
printf("Directory scan of %s\n",topdir);
ScanDir(topdir,0);
printf("done.\n");
exit(0);
}[/code]
[code]Win32 example 1: using directory handling functions
This sample prints out the current directory, and then recurses through subdirectories. It uses the FindFirstFile(), FindNextFile(), and FindClose() Win32 API functions.
#include <windows.h>
#include <stdio.h>
void ScanDir(char *dirname, int indent)
{
BOOL fFinished;
HANDLE hList;
TCHAR szDir[MAX_PATH+1];
TCHAR szSubDir[MAX_PATH+1];
WIN32_FIND_DATA FileData;
// Get the proper directory path
sprintf(szDir, "%s\\*", dirname);
// Get the first file
hList = FindFirstFile(szDir, &FileData);
if (hList == INVALID_HANDLE_VALUE)
{
printf("No files found\n\n");
}
else
{
// Traverse through the directory structure
fFinished = FALSE;
while (!fFinished)
{
// Check the object is a directory or not
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((strcmp(FileData.cFileName, ".") != 0) &&
(strcmp(FileData.cFileName, "..") != 0))
{
printf("%*s%s\\\n", indent, "",
FileData.cFileName);
// Get the full path for sub directory
sprintf(szSubDir, "%s\\%s", dirname,
FileData.cFileName);
ScanDir(szSubDir, indent + 4);
}
}
else
printf("%*s%s\n", indent, "", FileData.cFileName);
if (!FindNextFile(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
}
}
}
FindClose(hList);
}
void main(int argc, char *argv[])
{
char *pszInputPath;
char pwd[2] = ".";
if (argc < 2)
{
printf("Argument not supplied - using current directory.\n");
pszInputPath = pwd;
}
else
{
pszInputPath = argv[1];
printf("Input Path: %s\n\n", pszInputPath);
}
ScanDir(pszInputPath, 0);
printf("\ndone.\n");
}[/code]
これを見る通りopendirの移植にはFind~File系のAPIを推奨しているようです。
[/hide]