First to all, I found this source to get path of all files(Unicode) and it doesn't belong to me, I defined a string ( string str = "test"; ) and it's not detectable(cout << str;) in the function why is that happening?
#include <atlstr.h>
#include <iostream>
#include <string>
#include <functional>
#include <io.h>
using namespace std;
enum enumflags {
ENUM_FILE = 1,
ENUM_DIR,
ENUM_BOTH
};
bool enumsubfiles(
const std::wstring& dir_with_back_slant, //for example: L"C:\\", L"E:\\test\\"
const std::wstring& filename, //for example: L"123.txt", L"*.exe", L"123.???"
unsigned int maxdepth, //0 means not searching subdirectories, 1 means maximum depth of subdirectories is 1,
// pass -1 to search all the subdirectories.
enumflags flags, //search files, directories, or both.
std::function<bool(const std::wstring& dir_with_back_slant, _wfinddata_t& attrib)> callback)
{
_wfinddata_t dat;
size_t hfile;
std::wstring fullname = dir_with_back_slant + filename;
std::wstring tmp;
bool ret = true;
hfile = _wfindfirst(fullname.c_str(), &dat);
if (hfile == -1)
goto a;
do {
if (!(wcscmp(L".", dat.name) && wcscmp(L"..", dat.name)))
continue;
if (((dat.attrib & _A_SUBDIR) && (!(flags & ENUM_DIR))) || ((!(dat.attrib & _A_SUBDIR)) && (!(flags & ENUM_FILE))))
continue;
ret = callback(dir_with_back_slant, dat);
if (!ret) {
_findclose(hfile);
return ret;
}
} while (_wfindnext(hfile, &dat) == 0);
_findclose(hfile);
a:
if (!maxdepth)
return ret;
tmp = dir_with_back_slant + L"*";
hfile = _wfindfirst(tmp.c_str(), &dat);
if (hfile == -1)
return ret;
do {
if (!(wcscmp(L".", dat.name) && wcscmp(L"..", dat.name)))
continue;
if (!(dat.attrib & _A_SUBDIR))
continue;
tmp = dir_with_back_slant + dat.name + L"\\";
ret = enumsubfiles(tmp, filename, maxdepth - 1, flags, callback);
if (!ret) {
_findclose(hfile);
return ret;
}
} while (_wfindnext(hfile, &dat) == 0);
_findclose(hfile);
return ret;
}
int _tmain(int argc, _TCHAR* argv[])
{
string str = "test";
enumsubfiles(L"B:\\", L"*.txt", -1, ENUM_FILE, [](const std::wstring& dir_with_back_slant, _wfinddata_t& attrib) -> bool {
wcout << dir_with_back_slant << attrib.name << '\n';
cout << str;
return true; //return true to continue, return false to abort searching.
});
return 0;
}
Honestly, I do not understand anything from this source, I want to define a variable at the beginning of the int t_main() and call it inside the function and I don't want to define it in the function