2013-08-23 3 views
1

저는 C++을 처음 접했습니다. 그리고 폴더에서 이름을 취하는 함수를 작성하고 싶습니다.폴더에 파일 이름을 C++로 배열에 저장하는 방법은 무엇입니까?

예를 들어 나는 이름이 C : \ TEST 인 폴더를 가지고 있으며이 폴더에는 text.txt 파일이 많이 있는데 문자열 배열에 모든 .txt 파일 이름을 저장하려고합니다.

아무도이 문제에 관해 저에게 도움을 줄 수 없습니다.

내가 이런 식으로 뭔가를 시도하지만 난 부스트 파일 시스템을 사용

const int arr_size = 10; 
some_type src[arr_size]; 
// ... 
some_type dest[arr_size]; 
std::copy(std::begin(src), std::end(src), std::begin(dest)); 
+0

스택 자체를 탐색 할 수 있습니다. 너무 많은 돈이있다. http://stackoverflow.com/questions/612097/how-can-i-get-a-list-of-files-in : [1] [1] [여기에서 참조] -a-directory-using-c-or-c –

답변

3

실패 :

#include<vector> 
#include<string> 
#include <iostream> 
#include <iterator> 
#include <algorithm> 
#include <boost/filesystem.hpp> 
using namespace std; 
using namespace boost::filesystem; 

int main(int argc, char* argv[]) 
{ 
    vector<string> fnames; //your filenames will be stored here 

    path p (argv[1]); // C:\TEST 
    directory_iterator di(p); 
    directory_iterator di_end; 

    while(di != di_end) 
    { 
     fnames.push_back(di->path().filename().string()); 
     ++di; 
    } 
} 

위의 프로그램의 명령 행 인수로 C:\TEST를 지정합니다.

0
TCHAR szDir[MAX_PATH]; 
WIN32_FIND_DATA ffd; 
HANDLE hFind = INVALID_HANDLE_VALUE; 

hFind = FindFirstFile(szDir, &ffd); 

if (INVALID_HANDLE_VALUE != hFind) 
{ 
do 
    { 
    if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
    { 
    // You can do a recursive search here, the current file is a directory 
    } 
    src[arr_size] = ffd.cFileName; 
    arr_size++; 
    } 
    while (FindNextFile(hFind, &ffd) != 0); 


    FindClose(hFind); 
} 
+0

Windows API입니까? 그는 엄격한 Windows 솔루션을 찾고 있지 않았습니다. 그는 C++을 찾고 있었는데, 제게는 적절한 플랫폼을 의미합니다. – SevenBits

+0

@SevenBits, 그는 C++과 Windows의 태그를 넣습니다. – Pankaj

+0

그래서? 어쩌면 그는 그 플랫폼을 위해 컴파일하기 때문에 그 태그를 넣을 수도 있습니다. 어쩌면 그는 Windows에서 컴파일하고 있지만 여전히 휴대용 컴퓨터가 필요합니다. 누가 알아? 그는 ** C++ ** - Windows API가 아니라 (Windows API는 C로 작성되었으며 C++에서 C를 컴파일 할 수 있다는 것을 알고 있지만 C++을 사용하여 기능을 원한다고 가정하기 때문에 Windows API는 C로 작성되었습니다.) – SevenBits

관련 문제