I'm trying to call all existing ".stl" files in a given directory, and use them to build my geometry in Geant4. This is a separate program I've made to test it before I add it to my Geant4 code.
As for now, I've been trying to get the number of files via system command. The problem is that it always returns the correct number of files, 32, and then it returns Zero (0). Therefore, I cannot use it in a for loop. Furthermore, when I print the files, I have the "." and "..", which is not what I want.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstring>
#include <sys/types.h>
#include <dirent.h>
typedef std::vector<std::string> stringvec;
void read_directory(const std::string& name,stringvec& v)
{
DIR* dirp = opendir(name.c_str());
struct dirent * dp;
while ((dp = readdir(dirp)) != NULL) {
v.push_back(dp->d_name);
}
closedir(dirp);
}
int main()
{
int NbOfComponents;
stringvec v;
std::string Ext = ".stl";
std::string Dir = "/home/lghizoni/Geant4/SpaceRadiationFramework/Geometry/Satlab/";
std::string command = "ls -l "+Dir+"*"+Ext+"|wc -l";
const char *Command = command.c_str();
NbOfComponents = system(Command);
read_directory(Dir,v);
std::cout<<"The geometry is composed by "<<NbOfComponents<<" parts"<<std::endl;
std::copy(v.begin(),v.end(),std::ostream_iterator<std::string>(std::cout,"\n"));
First of all, I wanted to store the number of files in NbOfComponents, so I could use it in a loop later to call each of the parts. The problem is that it returns Zero as can be seen here in the output:
32
The geometry is composed by 0 parts
Assembly 5 - M2x10_TX6.stl
Assembly 5 - castor-v020 (3).stl
Assembly 5 - Sshield_bottom.stl
Assembly 5 - castor-v020 (7).stl
Assembly 5 - M2x10_TX6_1.stl
Assembly 5 - M2x10_TX6_12.stl
.
Assembly 5 - castor-v020 (2).stl
..
Assembly 5 - M2x10_TX6_14.stl
Assembly 5 - M2x10_TX6_9.stl
Assembly 5 - M2x10_TX6_7.stl
Assembly 5 - Sshield_top.stl
Assembly 5 - castor-v020 (1).stl
Assembly 5 - castor-v020 (11).stl
Assembly 5 - M2x10_TX6_13.stl
Assembly 5 - M2x10_TX6_10.stl
Assembly 5 - M2x10_TX6_3.stl
Assembly 5 - M2x10_TX6_8.stl
Assembly 5 - M2x10_TX6_5.stl
Assembly 5 - castor-v020 (12).stl
Assembly 5 - M2x10_TX6_2.stl
Assembly 5 - M2x10_TX6_11.stl
Assembly 5 - castor-v020 (5).stl
Assembly 5 - castor-v020 (13).stl
Assembly 5 - castor-v020 (6).stl
Assembly 5 - castor-v020 (14).stl
Assembly 5 - M2x10_TX6_4.stl
Assembly 5 - castor-v020 (8).stl
Assembly 5 - castor-v020 (9).stl
Assembly 5 - M2x10_TX6_6.stl
Assembly 5 - castor-v020 (4).stl
Assembly 5 - castor-v020.stl
Assembly 5 - castor-v020 (10).stl
The last point is that it returns "." and "..", which as I understand, are directories. With that I cannot call each of them from the vector v[i]. Any inputs will be most appreciated, and thanks in advance :)
system command doesn't return the out of the command applid it returns error code success / failure
check this from man page
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
DESCRIPTION
The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows:
execl("/bin/sh", "sh", "-c", command, (char *) 0);
system() returns after the command has been completed.
During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored, in the process that calls system() (these signals will be handled according to their defaults
inside the child process that executes command).
If command is NULL, then system() returns a status indicating whether a shell is available on the system
You can use popen command to get output of system command applied
FILE *fp;
char data[100];
std::string command = "ls -l "+Dir+"*"+Ext+"|wc -l";
const char *Command = command.c_str();
fp = popen(Command, "r");
while (fgets(data, sizeof(data), fp) != NULL)
{
printf("%s", data);
}
pclose(fp);
And about "." & ".." you should directly compare and ignore those two files as those will always be there presenting current and parent of current directory