在 C 中使用通配符查找文件名
Find file name using wildcard in C
我需要在 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);
这应该可以解决问题:
#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;
}
我需要在 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);
这应该可以解决问题:
#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;
}