c - C进入指定目录并仅查找简单文件,在第一个文件停止并执行某些操作
问题描述
我想使用 nftw() 并且只在第一个 lvl 深度。但它仍然更深入,即使我指定了 FTW_MOUNT。接下来我想检查文件是否是简单文件,并对其进行处理。这是我尝试过的:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <ftw.h>
int ftwinfo(const char *p, const struct stat *st, int fl, struct FTW *fbuf);
int ftwinfo(const char *p, const struct stat *st, int fl, struct FTW *fbuf){
static int cnt = 0;
puts( p );
cnt++;
if( cnt >= 10 ) return cnt;
return 0;
}
int main(int argc,char* argv[]){
int rv;
int flags = FTW_MOUNT;
rv = nftw( ".", ftwinfo, 20, flags );
if( rv == -1 ){
perror( "nftw failed" );
exit( 1 );
}
if( rv != 0 ){
printf( "ntfw fn function returnd %d != 0 -> nftw finished\n", rv );
}
return 0;
}
解决方案
推荐阅读
- qt - 自定义抓取物品的图像
- java - 为什么有和没有 ExecutorService 的任务花费相同的时间?
- java - 如何运行 Jenkins 并查看测试的可视化运行
- c# - Raise event in C# and catch in C++ MFC
- postgresql - 迭代插入值
- javascript - 无法重现查询参数 html 注入
- elasticsearch - 如何正确使用 SearchAfter API?
- spring-boot - 如何在 log4j2 配置中使用 Spring Boot application.yml 属性?
- c# - 如何使用序列化的二进制文件创建高分
- c# - 无法初始化 WebDriver Chrome 并且所有测试都失败了