读取目录下普通文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#define FILE_NAME_LEN 500
void read_dir(const char* file_name);
int opt_file(const char *file_name);
void read_dir(const char* file_name)
{
DIR *dir = NULL;
struct dirent* ent;
struct stat buf;
stat(file_name, &buf);
char path[FILE_NAME_LEN];
if (S_ISREG(buf.st_mode))
{
opt_file(file_name);
}
else if (S_ISDIR(buf.st_mode))
{
if ((dir = opendir(file_name)) == NULL)return;
while(ent = readdir(dir))
{
if (ent->d_name[0] == '.')
continue;
if (file_name[strlen(file_name)-1] == '/')
sprintf(path, "%s%s", file_name, ent->d_name);
else
sprintf(path, "%s/%s", file_name, ent->d_name);
read_dir(path);
}
}
}