Windows의 C를 사용하여 구조의 디렉토리 내용을 나열하고 저장하려고합니다.C 및 Windows를 사용하여 디렉토리 내용 나열
필자가 찾고있는 코드를 작성하는 사람을 찾고있는 것은 아니며, 내가보고 있어야하는 라이브러리에 관해서는 올바른 방향으로 가리키고 있습니다.
저는 지금 몇 시간 동안 인터넷 검색을 해왔고 모든 도움을 주시면 C#, C++ 솔루션을 사용하고 있습니다.
Windows의 C를 사용하여 구조의 디렉토리 내용을 나열하고 저장하려고합니다.C 및 Windows를 사용하여 디렉토리 내용 나열
필자가 찾고있는 코드를 작성하는 사람을 찾고있는 것은 아니며, 내가보고 있어야하는 라이브러리에 관해서는 올바른 방향으로 가리키고 있습니다.
저는 지금 몇 시간 동안 인터넷 검색을 해왔고 모든 도움을 주시면 C#, C++ 솔루션을 사용하고 있습니다.
다른 사람들이 말했다 (FindFirstFile을, FindNextFile과 및 FindClose와) ...하지만 재귀와 마찬가지로!
bool ListDirectoryContents(const char *sDir)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
char sPath[2048];
//Specify a file mask. *.* = We want everything!
sprintf(sPath, "%s\\*.*", sDir);
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
printf("Path not found: [%s]\n", sDir);
return false;
}
do
{
//Find first file will always return "."
// and ".." as the first two directories.
if(strcmp(fdFile.cFileName, ".") != 0
&& strcmp(fdFile.cFileName, "..") != 0)
{
//Build up our file path using the passed in
// [sDir] and the file/foldername we just found:
sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName);
//Is the entity a File or Folder?
if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
printf("Directory: %s\n", sPath);
ListDirectoryContents(sPath); //Recursion, I love it!
}
else{
printf("File: %s\n", sPath);
}
}
}
while(FindNextFile(hFind, &fdFile)); //Find the next file.
FindClose(hFind); //Always, Always, clean things up!
return true;
}
ListDirectoryContents("C:\\Windows\\");
그리고 지금은 유니 코드 대응 :
bool ListDirectoryContents(const wchar_t *sDir)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
wchar_t sPath[2048];
//Specify a file mask. *.* = We want everything!
wsprintf(sPath, L"%s\\*.*", sDir);
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
wprintf(L"Path not found: [%s]\n", sDir);
return false;
}
do
{
//Find first file will always return "."
// and ".." as the first two directories.
if(wcscmp(fdFile.cFileName, L".") != 0
&& wcscmp(fdFile.cFileName, L"..") != 0)
{
//Build up our file path using the passed in
// [sDir] and the file/foldername we just found:
wsprintf(sPath, L"%s\\%s", sDir, fdFile.cFileName);
//Is the entity a File or Folder?
if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
wprintf(L"Directory: %s\n", sPath);
ListDirectoryContents(sPath); //Recursion, I love it!
}
else{
wprintf(L"File: %s\n", sPath);
}
}
}
while(FindNextFile(hFind, &fdFile)); //Find the next file.
FindClose(hFind); //Always, Always, clean things up!
return true;
}
ListDirectoryContents(L"C:\\Windows\\");
파일 내용을 나열하려면 FindFirstFileEx, FindNextFile 및 CloseFind과 같은 디렉토리를 검색 할 수 있습니다. #include <windows.h
으로 설정하면 Windows API에 액세스 할 수 있습니다. 그것들은 C 함수이며 C++과 호환됩니다. "특별히 C++"을 원하면 MFC를 사용하여 디렉토리 목록을 검색해보십시오.
그는 MFC를 사용하고 있습니까? (그것은 악마입니다!) – NTDLS
잘 모르겠습니다. 나는 아무 팬도 아니지만 그것은 당신에게 물건에 대한 객체 지향 (OO) 관점을 제공합니다. –
당신의 C++ 솔루션은 API 당신이 할 필요가 부르는을 보여줍니다. –