首页 > 解决方案 > 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;
}

标签: cnftw

解决方案


推荐阅读