I want to get a file list in a folder, using glob.h
in C++.
Consider the following simple code:
#include <iostream>
#include "glob.h"
using namespace std;
int main(int argc, char const *argv[])
{
glob_t globResult;
string filePath = argv[1];
glob(filePath.c_str(),0,NULL,&globResult);
cout<<"No. of files found:"<<globResult.gl_pathc<<endl;
for(int i = 0; i < globResult.gl_pathc; i++)
{
cout << string(globResult.gl_pathv[i]) << endl;
}
return 0;
}
In a folder for following files:
Images\
|- cat.jpeg
|- dog.jpg
|- rat.jpg
when I run Image ./Images/*
, I only get cat.jpeg, instead of all three files.
What am I doing wrong?
Compiled using g++ 6.3
and clang++ 5.0
.
As per the comment... all command line args to an executable will, generally speaking, be interpreted by the shell before being passed to your app. So, given the file hierarchy...
Images\
|- cat.jpeg
|- dog.jpg
|- rat.jpg
The command...
Image ./Images/*
will actually result in...
Image ./Images/cat.jpeg ./Images/dog.jpg ./Images/rat.jpg
So the first parameter passed to glob
in your code will be ./Images/cat.jpeg
and, hence, that will be the only match. To avoid shell expansion simply quote the arg...
Image './Images/*'