Find files in folder question

Hello,

What I'm trying to do is listing all files in a specific directory, the program already knows the name and path of that current directory, so that doesn't needed to be searched specifically.

This is the code I don't understand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vector<string> get_all_files_names_within_folder(string folder)
{
    vector<string> names;
    char search_path[200];
    sprintf(search_path, "%s/*.*", folder.c_str());
    WIN32_FIND_DATA fd; 
    HANDLE hFind = ::FindFirstFile(search_path, &fd); 
    if(hFind != INVALID_HANDLE_VALUE) { 
        do { 
            // read all (real) files in current folder
            // , delete '!' read other 2 default folder . and ..
            if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
                names.push_back(fd.cFileName);
            }
        }while(::FindNextFile(hFind, &fd)); 
        ::FindClose(hFind); 
    } 
    return names;
}


I especially don't understand the search_path variable, and the sprintf function. Do I need those if I already know the directory path?

This is also a piece I don't really get:
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )

Would someone like to explain me this code?

Thanks for reading,
Niely
Do I need those if I already know the directory path?
Yes, although this is is preferable:
1
2
std::string search_path = folder + "*";
HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
folder should also be passed as reference to constant (const std::string &).

The if condition means "if the file is not a directory". It tests whether "is directory" bit of the attributes field is turned off.
???x?? & 000100 = 000x00
Examples: 12 & 4 = 4, 8 & 4 = 0, 3 & 5 = 1
Topic archived. No new replies allowed.