0

我正在尝试调用给定目录中的所有现有“.stl”文件,并使用它们在 Geant4 中构建我的几何图形。这是一个单独的程序,我在将它添加到我的 Geant4 代码之前对其进行测试。

至于现在,我一直在尝试通过系统命令获取文件数。问题是它总是返回正确数量的文件,32,然后它返回零 (0)。因此,我不能在 for 循环中使用它。此外,当我打印文件时,我有“。” 和“..”,这不是我想要的。

#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"));

首先,我想将文件的数量存储在 NbOfComponents 中,以便稍后在循环中使用它来调用每个部分。问题是它返回零,可以在输出中看到:

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

最后一点是它返回“。” 和“..”,据我了解,是目录。这样我就不能从向量 v[i] 中调用它们中的每一个。任何输入将不胜感激,并在此先感谢:)

4

2 回答 2

1

系统命令不返回命令应用程序的输出它返回错误代码成功/失败

从手册页检查这个

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

您可以使用 popen 命令获取应用的系统命令的输出

  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);

关于“。” & ".." 你应该直接比较并忽略这两个文件,因为它们总是存在当前目录和当前目录的父目录

于 2019-08-23T07:11:29.923 回答
1

所以第一个问题就是system不返回命令的输出,而是返回命令的退出码,退出码代表0命令执行成功。

第二个问题很容易解决,只需read_directory像这样更改您的功能

    v.push_back(dp->d_name);

    if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
        v.push_back(dp->d_name);

这意味着特殊目录...不会被添加到您的向量中。

现在要获取您可以执行此操作的组件数量

NbOfComponents = v.size();

但是,如果您的目录中有任何未命名的文件,这将不起作用*.stl。在这种情况下,请进一步修改read_directory,以便您不要使用push_back任何不以.stl

于 2019-08-23T07:10:58.517 回答