如何计算和存储向量中目录中的文件
How count and store files from a directory in a vector
我正在尝试调用给定目录中所有现有的“.stl”文件,并使用它们在 Geant4 中构建我的几何图形。这是一个单独的程序,我在将它添加到我的 Geant4 代码之前对其进行了测试。
至于现在,我一直在尝试通过系统命令获取文件数。问题是它总是 returns 正确的文件数,32,然后它 returns 零 (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 中,以便稍后在循环中使用它来调用每个部分。问题是它 returns 零,如输出所示:
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
最后一点是returns“.”和“..”,据我所知,是目录。这样我就不能从向量 v[i] 中调用它们中的每一个。
任何输入将不胜感激,并提前致谢:)
所以第一个问题是 system
没有 return 命令的输出,它 return 是 的退出代码 命令,退出代码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
结尾的文件名
系统命令没有return命令应用它returns错误代码成功/失败
从手册页检查这个
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);
关于“.” & ".." 你应该直接比较并忽略这两个文件,因为它们将始终存在,显示当前目录和当前目录的父目录
我正在尝试调用给定目录中所有现有的“.stl”文件,并使用它们在 Geant4 中构建我的几何图形。这是一个单独的程序,我在将它添加到我的 Geant4 代码之前对其进行了测试。
至于现在,我一直在尝试通过系统命令获取文件数。问题是它总是 returns 正确的文件数,32,然后它 returns 零 (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 中,以便稍后在循环中使用它来调用每个部分。问题是它 returns 零,如输出所示:
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
最后一点是returns“.”和“..”,据我所知,是目录。这样我就不能从向量 v[i] 中调用它们中的每一个。 任何输入将不胜感激,并提前致谢:)
所以第一个问题是 system
没有 return 命令的输出,它 return 是 的退出代码 命令,退出代码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
系统命令没有return命令应用它returns错误代码成功/失败
从手册页检查这个
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);
关于“.” & ".." 你应该直接比较并忽略这两个文件,因为它们将始终存在,显示当前目录和当前目录的父目录