1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <io.h>
#include <stdio.h>
#include <cstring>
#include <direct.h>    //windows 종속, 그런데 아래 있는 함수는 POSIX인 듯?
 
#include <string>
 
using namespace std;
 
vector<const char*> filePaths;
 
// based on legacybass (http://stackoverflow.com/questions/6133647/how-do-i-list-subdirectories-in-windows-using-c)
// dir 하부 디렉토리 폴더와 파일 이름들을 출력한다.
// 이 함수의 반환값은 반드시 unique_ptr로 받을것.
vector<const char*>& getFilePaths(const char* dir)
{
    auto* fileStrs = new vector<const char*>;
 
    char originalDirectory[_MAX_PATH];
 
    // Get the current directory so we can return to it
    _getcwd(originalDirectory, _MAX_PATH);
 
    if(_chdir(dir) == -1) { // Change to the working directory
        perror("Can't change directory!");
        exit(1);
    }
 
    _finddata_t fileinfo;
 
    // This will grab the first file in the directory
    // "*" can be changed if you only want to look for specific files
    intptr_t handle = _findfirst("*", &fileinfo);
 
    if(handle == -1){  // No files or directories found
        perror("Error searching for file");
        exit(1);
    }
 
    do {
        if(strcmp(fileinfo.name, "."== 0 || strcmp(fileinfo.name, ".."== 0) {
            continue;
        }
 
        string* nowPath = new string(dir);
        *nowPath += "\\";             //windows only
        *nowPath += fileinfo.name;
        
        if(fileinfo.attrib & _A_SUBDIR) { // Use bitmask to see if this is a directory
            auto subDir = getFilePaths(nowPath->c_str());
            fileStrs->insert( fileStrs->end(), subDir.begin(), subDir.end() ); //반환된 것을 뒤에 붙인다!
            delete nowPath; //폴더의 경로이기에 필요없음
        } else {
            fileStrs->push_back(nowPath->c_str());
        }
    } while(_findnext(handle, &fileinfo) == 0);
 
    _findclose(handle); // Close the stream
 
    _chdir(originalDirectory);
 
    return *fileStrs; 
}
cs

전역변수도 궁금해하더라구


(곧 클래스 멤버가 될 게시물입니다)