Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c+统计目录中存在的文件总数+; 我试图通过C++在UNIX操作系统中获得一个目录中的文件数 我有这个密码 int i; i = (int)system("ls -l /root/opencv/*.png|wc -l"); cout << "Number of files " << i << endl;_C++_Filesystems_Directory - Fatal编程技术网

使用c+统计目录中存在的文件总数+; 我试图通过C++在UNIX操作系统中获得一个目录中的文件数 我有这个密码 int i; i = (int)system("ls -l /root/opencv/*.png|wc -l"); cout << "Number of files " << i << endl;

使用c+统计目录中存在的文件总数+; 我试图通过C++在UNIX操作系统中获得一个目录中的文件数 我有这个密码 int i; i = (int)system("ls -l /root/opencv/*.png|wc -l"); cout << "Number of files " << i << endl;,c++,filesystems,directory,C++,Filesystems,Directory,如何在I中获取21,系统调用返回UNIX中shell的退出状态。因此,它返回0是有意义的 如果要获取文件计数,需要解析系统函数的输出。否则,使用系统调用来计算目录中需要多少PNG文件 看看opendir和readdir函数。最好使用这些函数,而不是解析系统输出。这是意料之中的。该系统称: 返回值 错误时返回的值为-1(例如fork(2)失败),命令的返回状态为-1 你真的不想在这里调用system和ls,标准的方法是对整个系统进行调用,或者如果你只是查找文件名模式的话,则是直接调用 如果您坚持要

如何在
I

中获取
21
系统
调用返回UNIX中shell的退出状态。因此,它返回
0
是有意义的

如果要获取文件计数,需要解析
系统
函数的输出。否则,使用系统调用来计算目录中需要多少PNG文件


看看
opendir
readdir
函数。最好使用这些函数,而不是解析
系统
输出。

这是意料之中的。该系统称:

返回值

错误时返回的值为-1(例如fork(2)失败),命令的返回状态为-1

你真的不想在这里调用system和ls,标准的方法是对整个系统进行调用,或者如果你只是查找文件名模式的话,则是直接调用


如果您坚持要生成三个进程来计算目录中的文件数,那么您应该查看以读取命令的输出。

之所以会这样,是因为您得到的是命令的返回值,而不是输出

如果要使用系统命令
ls
,而不是像其他人建议的那样使用
opendir
readdir
,则应使用
popen
而不是
系统

#include <stdio.h>

int main() 
{
    FILE *in;
    char buff[512];

    /* popen creates a pipe so we can read the output
       of the program we are invoking */
    if (!(in = popen("ls -l /root/opencv/*.png|wc -l", "r"))) 
    {  
        /* if popen failed */
        return 1;
    }

    /* read the output of ls, one line at a time */
    while (fgets(buff, sizeof(buff), in) != NULL ) 
    {
        printf("Number of files: %s", buff);
    }

    /* close the pipe */
    pclose(in);
    return 0;
}
#包括
int main()
{
文件*in;
字符buff[512];
/*popen创建一个管道,以便我们可以读取输出
我们正在调用的程序的*/
如果(!(in=popen(“ls-l/root/opencv/*.png | wc-l”,“r”))
{  
/*如果popen失败了*/
返回1;
}
/*读取ls的输出,一次读取一行*/
while(fgets(buff,sizeof(buff),in)!=NULL)
{
printf(“文件数:%s”,buff);
}
/*关上管子*/
pclose(in);
返回0;
}

使用glob(2)功能可以非常轻松地实现您想要的:

#include <glob.h>
int glob(const char *pattern, int flags,
                int (*errfunc) (const char *epath, int eerrno),
                glob_t *pglob);
#包括
int glob(常量字符*模式,int标志,
int(*errfunc)(常量字符*epath,int eerrno),
glob_t*pglob);
简单示例(无错误处理):

glob\t gl;
大小\u t num=0;
if(glob(“/root/opencv/*.png”,glob_NOSORT,NULL,&gl)==0)
num=gl.gl\u路径c;
globfree&gl;

cout尽管您指定了一个操作系统,但可能需要一个可移植的解决方案

Boost::文件系统,这是您所寻找的。
count\u if
的谓词可以使用
std::regex
或任何适合您的内容

下面是一个展示所需行为的最小示例(不包括递归):

#包括
#包括
#包括
名称空间fs=boost::filesystem;
int main()
{
int i=std::count\u如果(fs::directory\u迭代器(“/your/path/here/”),则,
fs::directory_迭代器(),
[](常量fs::目录项&e){
返回e.path().extension()=“.png”;});
/还考虑递归递归算法

STD::CUT+1抱歉,我是新的,因为我在OpenCV上工作,所以对C++不太熟悉,但是解析你所说的不是代码中的代码>代码= i=(int)系统(“ls -L/root / opencv/*.png.WC-L”);< /Cord>我在哪里使用(int)解析。@wazzy:您需要解析系统函数调用输出。或者使用类似于
execv
的方法。我建议您使用
opendir
readdir
来完成您试图完成的任务…@wazzy:使用
glob
查看答案。它似乎正是您想要的。运行时的结果是什么bash ls-l/root/opencv/*.png | wc-l上的命令应该使用
I=(int)popen(“ls-l/root/opencv/*.png | wc-l”,“r”);
我应该使用
I=(int)popen(“ls-l/root/opencv/*.png | wc-l”,“r”);
遗憾的是,事情并没有那么简单(毕竟这是C…).Popen将向您返回一个FILE*对象,您可以从中加载到一个缓冲区中,然后从中加载atoi或fscanf…酷答案。不知道存在
glob
#include <glob.h>
int glob(const char *pattern, int flags,
                int (*errfunc) (const char *epath, int eerrno),
                glob_t *pglob);
glob_t gl;
size_t num = 0;
if(glob("/root/opencv/*.png", GLOB_NOSORT, NULL, &gl) == 0)
  num = gl.gl_pathc;
globfree(&gl);
cout << "Number of files: " << num << endl;
#include <boost/filesystem.hpp>
#include <iostream>
#include <algorithm>

namespace fs = boost::filesystem;

int main()
{
  int i =  std::count_if(fs::directory_iterator("/your/path/here/"),
                         fs::directory_iterator(), 
                         [](const fs::directory_entry& e) { 
                          return e.path().extension() == ".png";  });
  //also consider recursive_directory_iterator
  std::cout << i << std::endl;
  return 0;
}