在c++ /C中,在Windows中

时间:2022-09-01 23:39:10

Is there a smooth way 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]=*.txt in it.

例如,myprogram。exe *。txt会给我的程序发送一个ARGV列表,其中包含…ARGV[1]=*。三种。

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.txt in 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?

这样的存在吗?

5 个解决方案

#1


23  

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

与setargv链接。obj(或wsetargv.obj)和argv[]将作为全局变量,类似于Unix shell的做法:

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

我不能保证它有多好。

#2


3  

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

#3


2  

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

有人曾说过将它放在Boost:::文件系统中,但后来放弃使用Boost:: regex。

For win32 specific (MFC) you can use the CFileFind class

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

#4


1  

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 library statically 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 here if you like.

可能会有一个更好的方法了,但上次我不得不处理这个问题我最终包括亨利·斯宾塞的正则表达式库静态链接到我的程序(他的图书馆是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 wrapper class for that also...

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

#5


0  

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例程开始。Globs并不是RegExs,因此您必须实现自己的过滤。

#1


23  

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

与setargv链接。obj(或wsetargv.obj)和argv[]将作为全局变量,类似于Unix shell的做法:

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

我不能保证它有多好。

#2


3  

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

#3


2  

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

有人曾说过将它放在Boost:::文件系统中,但后来放弃使用Boost:: regex。

For win32 specific (MFC) you can use the CFileFind class

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

#4


1  

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 library statically 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 here if you like.

可能会有一个更好的方法了,但上次我不得不处理这个问题我最终包括亨利·斯宾塞的正则表达式库静态链接到我的程序(他的图书馆是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 wrapper class for that also...

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

#5


0  

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例程开始。Globs并不是RegExs,因此您必须实现自己的过滤。