简体   繁体   English

在 C 中接受带有通配符的文件名的跨平台“标准”方式?

[英]Cross-platform “standard” way to accept file names with wildcards in C?

I'm writing a command line utility in C to process image files.我正在 C 中编写一个命令行实用程序来处理图像文件。 In its current state, the application can accept multiple explicit file names though argv , but I'd like to add support for wildcards, like this:在其当前的 state 中,应用程序可以通过argv接受多个显式文件名,但我想添加对通配符的支持,如下所示:

imgprocess.exe *.png *.gif

or或者

./imgprocess *.png *.gif

It seems like this should be a common enough thing to be supported by C99, but I'm having a very difficult time finding a standard, cross-platform solution.看起来这应该是 C99 支持的足够普遍的东西,但是我很难找到一个标准的跨平台解决方案。

For Windows, it appears (via this article ) that linking to setargv.obj does the trick, but that's specific to Windows and I think Visual Studio.对于 Windows,似乎(通过本文)链接到setargv.obj可以解决问题,但这是 Windows 特有的,我认为是 Visual Studio。

For Linux, it looks like readdir() or scandir() can get me a directory listing and I can iterate through that to match files, but I think that's just Linux.对于 Linux,看起来readdir()scandir()可以给我一个目录列表,我可以遍历它来匹配文件,但我认为这只是 Linux。

Since wildcards are such a common thing, I feel like I'm missing some kind of simple obvious solution.由于通配符是如此普遍,我觉得我错过了某种简单明显的解决方案。 Currently I'm on Linux compiling with gcc, but I would like it to compile for at least Windows and Linux.目前我正在使用 Linux 编译 gcc,但我希望它至少可以编译为 Windows 和 ZEDC8318A5A36D57473E。

Thank you for any advice.谢谢你的任何建议。

On Linux-like systems at least, the shell expands the wildcard into a list of filenames.至少在类似 Linux 的系统上,shell 将通配符扩展为文件名列表。 So there is no need to add this functionality!所以没有必要添加这个功能!

The normal way to handle this problem in a UNIX or UNIX-like environment is to not handle it at all.在 UNIX 或类 UNIX 环境中处理此问题的正常方法是根本不处理它。 That is, let the shell do the glob expansion.也就是让shell做glob扩展。 Your program just gets a big list of files and doesn't have to worry about looking around in the filesystem at all.你的程序只是得到一个大的文件列表,根本不用担心在文件系统中四处寻找。 A quick example program:一个快速的示例程序:

#include <stdio.h>

int main(int argc, char **argv)
{
  int i;

  for (i = 1; i < argc; i++)
  {
    printf("%s\n", argv[i]);
  }

  return 0;
}

And a couple of sample runs (the only files in the directory are example and example.c ):还有几个示例运行(目录中的唯一文件是exampleexample.c ):

$ ./example 
$ ./example *
example
example.c
$ ./example *.c
example.c

POSIX systems do support glob and globfree functions (try man 3 glob , becuase just man glob may get you the shell documentation) which do the same thing as the shell's glob behavior. POSIX 系统确实支持globglobfree函数(尝试man 3 glob ,因为只有man glob可能会为您提供 shell 文档),它们与 shell 的 glob 行为相同。

However--as others have said--it is not really common or expected that your program will process arguments that way: the user will have to quote the arguments to get them to you from the commend line.然而——正如其他人所说——你的程序以这种方式处理 arguments 并不是很常见或预期的:用户必须引用 arguments 才能从推荐行中获取它们。


If you want to be really cross-platform you may want to implement this functionality yourself.如果你想真正跨平台,你可能想自己实现这个功能。 Say using PCRE (the Perl Compatible Regular Expression library) as the basis since this is available on all common platforms.假设使用 PCRE(Perl 兼容正则表达式库)作为基础,因为它在所有常见平台上都可用。

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

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