在 C++/C 中通配,在 Windows 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1269480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:59:15  来源:igfitidea点击:

Globbing in C++/C, on Windows

c++cwindowsglob

提问by Paul Nathan

Is there a smoothway to glob in C or C++ in Windows?

有没有一种流畅的方法可以在 Windows 中使用 C 或 C++?

E.g., myprogram.exe *.txt sends my program an ARGV list that has...ARGV[1]=*.txtin it.

例如,myprogram.exe *.txt 向我的程序发送一个 ARGV 列表,其中包含...ARGV[1]= *.txt

I would like to be able to have a function (let's call it readglob) that takes a string and returns a vector of strings, each containing a filename.

我希望能够有一个函数(我们称之为 readglob),它接受一个字符串并返回一个字符串向量,每个字符串都包含一个文件名。

This way, if I have files a.txt b.txt c.txtin my directory and readglob gets an argument *.txt, it returns the above filelist.

这样,如果a.txt b.txt c.txt我的目录中有文件并且 readglob 得到一个参数*.txt,它会返回上面的文件列表。

//Prototype of this hypothetical function.
vector<string> readglob(string);

Does such exist?

这样的存在吗?

回答by Michael Burr

Link with setargv.obj(or wsetargv.obj) and argv[] will be globbed for you similar to how the Unix shells do it:

Link with setargv.obj(or wsetargv.obj) 和 argv[] 将为您提供类似于 Unix shell 的方法:

I can't vouch for how well it does it though.

不过,我不能保证它做得有多好。

回答by Martin Beckett

there was talk about having it in Boost::filesystem but it was dropped in favor of using the boost::regex.

有谈论在 Boost::filesystem 中使用它,但它被放弃以支持使用 boost::regex。

For win32 specific (MFC) you can use the CFileFindclass

对于 win32 特定(MFC),您可以使用CFileFind

回答by A.J.

This is very Windows-specific. I don't know how you'd write this to be cross-platform. But I've used this in Windows programs and it works well for me.

这是非常特定于 Windows 的。我不知道你怎么写这个是跨平台的。但是我已经在 Windows 程序中使用过它,它对我来说效果很好。

// Change to the specified working directory
string path;
cout << "Enter the path to report: ";
cin >> path;
_chdir(path.c_str());

// Get the file description
string desc;
cout << "Enter the file description: ";
cin >> desc;

// List the files in the directory
intptr_t file;
_finddata_t filedata;
file = _findfirst(desc.c_str(),&filedata);
if (file != -1)
{
  do
  {
    cout << filedata.name << endl;
    // Or put the file name in a vector here
  } while (_findnext(file,&filedata) == 0);
}
else
{
  cout << "No described files found" << endl;
}
_findclose(file);

回答by Jeremy Friesner

There may be a better way now, but last time I had to deal with this problem I ended up including Henry Spencer's regex librarystatically linked into my program (his library is BSD licensed), and then I made a wrapper class that converted the user's glob-expressions into regular expressions to feed to the regex code. You can view/grab the wrapper class hereif you like.

现在可能有更好的方法,但上次我不得不处理这个问题时,我最终将Henry Spencer 的正则表达式库静态链接到我的程序中(他的库是 BSD 许可的),然后我制作了一个包装类来转换用户的glob-expressions 转换为正则表达式以提供给正则表达式代码。如果您愿意,可以在此处查看/获取包装器类。

Once you have those parts in place, the final thing to do is actually read the directory, and pass each entry name into the matching function to see if it matches the expression or not. The filenames that match, you add to your vector; the ones that don't you discard. Reading the directory is fairly straightforward to do using the DOS _findfirst() and _findnext() functions, but if you want a nicer C++ interface I have a portable wrapperclass for that also...

一旦您准备好这些部分,最后要做的就是读取目录,并将每个条目名称传递给匹配函数以查看它是否与表达式匹配。匹配的文件名,您添加到您的向量中;那些你不丢弃的。使用 DOS _findfirst() 和 _findnext() 函数读取目录相当简单,但是如果你想要一个更好的 C++ 接口,我也有一个可移植的包装类......

回答by Roboprog

Ehw. I had to implement something like this in ANSI C about 15 years ago. Start with the ANSI opendir/readdir routines, I guess. Globs aren't exactly RegExs, so you will have to implement your own filtering.

嗯。大约 15 年前,我不得不在 ANSI C 中实现类似的东西。我猜从 ANSI opendir/readdir 例程开始。Glob 不完全是 RegEx,因此您必须实现自己的过滤。