简体   繁体   English

以可移植的方式在C ++中读取目录

[英]reading a directory in C++ in portable way

I have a requirement as follows. 我有如下要求。 I have a directory for eg i have /tmp/myFolder 我有一个目录,例如我有/ tmp / myFolder

Under "myFolder" i have files whose names as below 在“ myFolder”下,我有一些文件,其名称如下

myFile-N001.txt
myFile-N002.txt
myFile-N003.txt

in my application while writing to a file i should write to largest number file in /tmp/myFolder, for eg in above case i should write to myFile-N003.txt. 在我的应用程序中,在写入文件时,我应该写入/ tmp / myFolder中最大数量的文件,例如,在上述情况下,我应该写入myFile-N003.txt。

The above should be achieved in C++ and in portable way preferably in POSIX way. 以上应该以C ++以及可移植的方式(最好以POSIX的方式)实现。

Can any one provide simple example on how we can achive this in C++. 谁能提供一个简单的示例说明如何在C ++中实现这一目标。

Thanks 谢谢

If you are happy enough to: 如果您足够高兴:

  • work only on POSIX 仅在POSIX上工作
  • You know the pattern you are looking for 您知道要寻找的图案

You can use the glob() function in POSIX to get the list. 您可以在POSIX中使用glob()函数来获取列表。

You can then use std::max with a custom comparison on the result set with your iterator range being globres.gl_pathv[0], globres.gl_pathv[globres.gl_pathc] and a comparison being something like: 然后,您可以使用std :: max对结果集进行自定义比较,并且迭代器范围为globres.gl_pathv [0],globres.gl_pathv [globres.gl_pathc],并且比较如下:

bool strless(const char* str1, const char* str2 )
{
   return strcmp( str1, str2 ) < 0;
}

although there is most likely something already that does that 尽管最有可能已经做到了

The start of the "man" page for glob: glob的“ man”页面的开始:

   #include <glob.h>

   int glob(const char *pattern, int flags,
            int (*errfunc) (const char *epath, int eerrno),
            glob_t *pglob);

   void globfree(glob_t *pglob);

DESCRIPTION 描述

The glob() function searches for all the pathnames matching pattern according to the rules used by the shell (see glob (7)). glob()函数根据外壳使用的规则搜索所有与模式匹配的路径名(请参阅glob (7))。 No tilde expansion or parameter substitution is done; 没有波浪号扩展或参数替换; if you want these, use wordexp (3). 如果需要这些,请使用wordexp (3)。

The globfree() function frees the dynamically allocated storage from an earlier call to glob() . globfree()函数将动态分配的存储从先前对glob()调用中释放出来。

The results of a glob() call are stored in the structure pointed to by pglob. glob()调用的结果存储在pglob指向的结构中。 This structure is of type glob_t (declared in <glob.h> ) and includes the following elements defined by POSIX.2 (more may be present as an extension): 此结构的类型为glob_t (在<glob.h>声明),包括POSIX.2定义的以下元素(可能会作为扩展出现):

 typedef struct 
 {
     size_t   gl_pathc;    /* Count of paths matched so far  */
     char   **gl_pathv;    /* List of matched pathnames.  */
     size_t   gl_offs;     /* Slots to reserve in gl_pathv.  */
 } glob_t;

There was a discussion in October 2004 about bringing glob into boost I don't think it ever got implemented: 在2004年10月,有一次关于将glob引入提升的讨论 ,我认为它从未实现过:

There's code for retrieving the list of files from a directory in a POSIX-compliant way here . 有在POSIX兼容的方式,从目录中检索文件列表的代码在这里

You could build on that by sorting the resulting array and popping the last element off. 您可以通过对结果数组进行排序并弹出最后一个元素来建立此基础。 That would be the file you want to find. 那就是您要查找的文件。

您可以使用boost :: filesystem来实现。

This is really two problems in one. 这实际上是两个问题合二为一。 I'd use Boost.Filesystem to portably iterate over the files and store them in a vector with ( untested ) 我将使用Boost.Filesystem可移植地遍历文件,并将它们存储在具有( 未经测试 )的vector

std::string maxname;
unsigned maxnum = 0;

for (fs::path::iterator i(dir.begin()), end(dir.end()); i != end; ++i) {
    std::string name = i->string();
    unsigned num;
    if (std::sscanf(name.c_str(), "myFile-N%u.txt", &num) == 1
     && num >= maxnum) {
        maxnum = num;
        maxname = name;
    }
}

then check whether maxname holds the empty string after the loop. 然后检查循环后maxname是否包含空字符串。 If it doesn't, it holds the filename you want. 如果没有,它将保存所需的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM