저는 프로젝트에서 실제로 작업하고 있습니다. 디렉토리를 찾아야하는데, 사용하고 싶지 않기 때문에 dirent.h 라이브러리를 사용하고 있습니다. 그것을 위해 부스트하십시오.
따라서이 게시물 <dirent.h> in visual studio 2010 or 2008은 어디로 이어졌으며 http://www.softagalleria.net/dirent.php dirent.h를 다운로드하여 설치했습니다.
dirent.h가 설치되어 있으므로 opendir, readdir과 같은 기본 기능을 사용해도 문제가 없지만 seekdir() 함수를 사용하려고 할 때 라이브러리에 존재하지 않는 것 같아서 확인하기 위해 dirent.h에 들어갔습니다. 내 가설과 (감사 Ctrl + F) seekdir 실종입니다.
내가 뭔가를 놓치거나이 기능을 얻으려면 트릭을 찾아야합니까 ...?
감사합니다. 해당 헤더에서 사용할 수dirent.h에서 seekdir()을 찾을 수 없습니다. Windows 용 (C++)
답변
유일한 기능은 다음과 같습니다 당신이 필요로하는 기능을 얻기 위해 어떤 트릭이
DIR *opendir (const char *dirname);
struct dirent *readdir (DIR *dirp);
int closedir (DIR *dirp);
void rewinddir (DIR* dirp);
없습니다. 이를 위해 다른 라이브러리를 찾아야합니다.
당신이 헤더 파일 dirent.h
을 찾을 못할 경우 대안으로 WIN32_FIND_DATA
, FindFirstFile()
및 FindNextFile()
를 사용해보십시오. 두 개의 다른 코드가 제출됩니다. 하나는 Visual Studio 6.0 용이고 다른 하나는 와이드 문자를 사용해야하는 Visual Studio 2013 용입니다. 비주얼 스튜디오 6.0
코드 : 비주얼 스튜디오 2013
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
void listdirandfiles(string dir){
HANDLE hFind;
WIN32_FIND_DATAA data;
hFind = FindFirstFileA(dir.c_str(), &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
printf("%s\n", data.cFileName);
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
}
int main(int argc, char** argv){
string dir = "c:\\*.*";
cout<<"\nListing directories or files..\n\n";
listdirandfiles(dir);
cout<<"\nPress ANY key to close.\n\n";
cin.ignore(); cin.get();
return 0;
}
코드 :
// visual studio 2013
// listdirConsoleApplication15.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
{
wchar_t* wString = new wchar_t[4096];
MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
return wString;
}
void listdirandfiles(char *wstr){
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(convertCharArrayToLPCWSTR(wstr), &FindFileData);
do{
_tprintf(TEXT("%s\n"), FindFileData.cFileName);
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
int main()
{
char *wstr = "c:\\*.*";
cout << "\nListing directories or files..\n\n";
listdirandfiles(wstr);
cout << "\nPress ANY key to close.\n\n";
cin.ignore(); cin.get();
return 0;
}
WRT "와이드 문자 [,]"를 사용해야합니다. 일반적으로 Windows는 와이드 문자에 대한 'W'접미사와 good ol '8 비트 ANSI에 대한 접미사'A '가있는 두 가지 기능을 제공합니다. 주어진 함수는 'W'버전을 가리 키도록 정의됩니다. 예 : 'FindFirstFile'은'FindFirstFileW'로 정의됩니다. 'FindFirstFileA'는 직접 또는 어딘가에서 호출 할 수 있습니다. 전역 적으로 'W'에서 'A'로 리디렉션 할 컴파일러 옵션이 있습니다. 그래도 내가 그 옵션을 기억할 수만 있다면. – user4581301
당신은 * * 표시 헤더 파일의 차이 *와 * 라이브러리를 배우고 싶어요. 헤더를 다운로드하는 것만으로는 충분하지 않습니다. – tofro
좀 더 설명해 주시겠습니까? 나는 작은 튜토리얼을 따라야하고 dirent.h의 함수를 사용할 수 있으며 그 함수가 실제로 작동한다는 것을 의미합니다./ – Freddykong
헤더 전용 라이브러리이므로 .h 파일 만 필요합니다. 'seekdir'은 단순히 저자가 구현하지 않은 것 같습니다. – Ari0nhh