【问题标题】:Find file name using wildcard in C在 C 中使用通配符查找文件名
【发布时间】:2018-09-19 09:52:40
【问题描述】:

我需要在 C 程序中使用 * 来查找文件的名称。特定文件夹中恰好有 1 个文件扩展名为 .UBX。我可以在终端中执行此操作,但它在 C 中不起作用。谁能给我示例代码来执行此操作?

//There is exactly 1 file that ends in .UBX
#define FILE_TO_SEND    "/home/root/logs/*.UBX"

fd = open(FILE_TO_SEND, O_RDONLY);

【问题讨论】:

标签: c linux wildcard


【解决方案1】:

这应该可以解决问题:

#include <glob.h>
#include <stdlib.h>
#include <fcntl.h>

#define FILE_TO_SEND "/home/root/logs/*.UBX"

int
main (void)
{
    glob_t globbuf;

    glob(FILE_TO_SEND, 0, NULL, &globbuf);

    if (globbuf.gl_pathc > 0)
    {
        int fd = open(globbuf.gl_pathv[0], O_RDONLY);

        // ...
    }

    globfree(&globbuf);

    return 0;
}

【讨论】:

  • 这不是 OP 要求的吗?
猜你喜欢
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多