I have a function that returns content of folder:
void getDir(const char* d, vector<string> & f)
{
FILE* pipe = NULL;
string pCmd = "dir /B /S " + string(d);
char buf[256];
if( NULL == (pipe = _popen(pCmd.c_str(),"rt")))
{
cout<<"[Expletive deleted]"<<endl;
return;
}
while (!feof(pipe))
{
if(fgets(buf,256,pipe) != NULL)
{
f.push_back(string(buf));
}
}
_pclose(pipe);
}
The problem is that string pCMD
should be equal to "dir /B /S c:\Users\Tom"
, but after converting char to string it removes the backslashes like "dir /B /S c:UsersTom"
. I've tried calling the function in these ways:
getDir("c:\Users\Tom", files);
getDir("c:/Users/Tom", files);
getDir("c:\\Users\Tom", files);
getDir("c:\\Users\\Tom", files);
But every time I get a failure (vector files
is empty...). Does anybody know what I'm doing wrong?