诚求unix环境下标准C实现的遍历某个文件夹下面所有文件的代码
找了半天,都是C++的,需要纯C的,大家多帮忙!
[解决办法]
去看
findfirt
findnext
这2个posix函数的说明。
[解决办法]
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
int testdir(char *path)
{
struct stat buf;
if(lstat(path,&buf) <0)
{
return 0;
}
if(S_ISDIR(buf.st_mode))
{
return 1; //directory
}
return 0;
}
int directory(char *path)
{
DIR *db;
char filename[128];
struct dirent *p;
db=opendir(path);
if(db==NULL)return 0;
memset(filename,0,128);
while ((p=readdir(db)))
{
if((strcmp(p-> d_name, ". ")==0)||(strcmp(p-> d_name, ".. ")==0))
continue;
else
{
sprintf(filename, "%s/%s ",path,p-> d_name);
if(testdir(filename))
{
directory(filename);
}
else {
printf( "%s\n ",filename);
}
}
memset(filename,0,64);
}
closedir(db);
return 0;
}
int main(int argc,char **argv)
{
char *path= "./ "; //要遍历的目录
if(access(path,F_OK)==0&&testdir(path))
{
printf( "is directory\n ");
directory(path);
}
else printf( "%s not exist\n ",path);
}
[解决办法]
man ftw
[解决办法]
ftw
[解决办法]
to:taodm((不能收CSDN社区短信息,请莫浪费精力))
findfirst
findnext
是posix标准的吗?好像是MS的吧,有谁确定一下?
[解决办法]
dos的
[解决办法]
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
int main(int argc, char **argv) {
DIR *dir = NULL;
dirent *ent = NULL;
dir = opendir( "Release ");
if (NULL == dir) {
printf( "Release is not a dir. ");
}
while (NULL != (ent = readdir(dir))) {
printf( "%s: %ld\n ", ent-> d_name, ent-> d_ino);
}
closedir(dir);
}