当前位置: 技术问答>linux和unix
急求!取已知目录的文件名和路径的问题(近期结贴)
来源: 互联网 发布时间:2015-07-25
本文导语: 想取一个目录下(包含子目录)所有文件的文件名和路径,分别对应存储于数组char *sFileName[]和char *sFilePath[]中,请问该怎么做? 谢谢了 m_( )_m | #include #include int ftw (path, fn, dep...
想取一个目录下(包含子目录)所有文件的文件名和路径,分别对应存储于数组char *sFileName[]和char *sFilePath[]中,请问该怎么做?
谢谢了
m_( )_m
谢谢了
m_( )_m
|
#include
#include
int ftw (path, fn, depth)
char *path;
int (*fn) ();
int depth;
Description
===========
The ftw function recursively descends the directory hierarchy rooted in path.
For each object in the hierarchy, ftw calls fn, passing it a pointer to a
null-terminated character string containing the name of the object, a
pointer to a stat structure (see stat(S)) containing information about the
object, and an integer. Possible values of the integer, defined in the
header file, are:
FTW_F file
FTW_D directory
FTW_DNR
directory that cannot be read
FTW_NS object for which stat could not successfully be executed.
这个函数可以得到指定路径下的所有目录的路径以及其访问权限。
#include
int ftw (path, fn, depth)
char *path;
int (*fn) ();
int depth;
Description
===========
The ftw function recursively descends the directory hierarchy rooted in path.
For each object in the hierarchy, ftw calls fn, passing it a pointer to a
null-terminated character string containing the name of the object, a
pointer to a stat structure (see stat(S)) containing information about the
object, and an integer. Possible values of the integer, defined in the
header file, are:
FTW_F file
FTW_D directory
FTW_DNR
directory that cannot be read
FTW_NS object for which stat could not successfully be executed.
这个函数可以得到指定路径下的所有目录的路径以及其访问权限。
|
刚找的,有点乱,凑合看吧
不行的话在google上搜吧,关键词“readdir linux 目录”
--------------------------------------------------------------------------------
opendir/readdir/closedir/rewinddir : 读取目录信息
--------------------------------------------------------------------------------
#include
#include
DIR * opendir(const char * pathname);
int closedir(DIR *dir);
struct dirent * readdir(DIR *dir);
int rewinddir(DIR *dir);
struct dirent {
long d_ino; /* inode number */
off_t d_off; /* offset to this dirent */
unsigned short d_reclen; /* length of this d_name */
char d_name [NAME_MAX+1]; /* file name (null-terminated) */
};
opendir开启一个目录操作DIR,closedir关闭之。
readdir则循序读取目录中的资讯,rewinddir则可重新读取目录资讯。
以下是个标准例。
--------------------------------------------------------------------------------
#include
#include
char ** dirGetInfo(const char *pathname)
{
char ** filenames;
DIR * dir;
struct dirent * ent;
int n = 0;
filenames = (char **)malloc(sizeof(char*));
filenames[0]=NULL;
dir = opendir(pathname);
if (!dir) return filenames;
while ((ent = readdir(dir))) {
filenames = (char**)realloc(filenames,sizeof(char*)*(n+1));
filenames[n] = strdup(ent->d_name);
n++;
}
closedir(dir);
filenames = (char **)realloc(filenames,sizeof(char*)*(n+1));
filenames[n] = NULL;
return filenames;
}
不行的话在google上搜吧,关键词“readdir linux 目录”
--------------------------------------------------------------------------------
opendir/readdir/closedir/rewinddir : 读取目录信息
--------------------------------------------------------------------------------
#include
#include
DIR * opendir(const char * pathname);
int closedir(DIR *dir);
struct dirent * readdir(DIR *dir);
int rewinddir(DIR *dir);
struct dirent {
long d_ino; /* inode number */
off_t d_off; /* offset to this dirent */
unsigned short d_reclen; /* length of this d_name */
char d_name [NAME_MAX+1]; /* file name (null-terminated) */
};
opendir开启一个目录操作DIR,closedir关闭之。
readdir则循序读取目录中的资讯,rewinddir则可重新读取目录资讯。
以下是个标准例。
--------------------------------------------------------------------------------
#include
#include
char ** dirGetInfo(const char *pathname)
{
char ** filenames;
DIR * dir;
struct dirent * ent;
int n = 0;
filenames = (char **)malloc(sizeof(char*));
filenames[0]=NULL;
dir = opendir(pathname);
if (!dir) return filenames;
while ((ent = readdir(dir))) {
filenames = (char**)realloc(filenames,sizeof(char*)*(n+1));
filenames[n] = strdup(ent->d_name);
n++;
}
closedir(dir);
filenames = (char **)realloc(filenames,sizeof(char*)*(n+1));
filenames[n] = NULL;
return filenames;
}
|
man ftw
man nftw
看这2个函数
man nftw
看这2个函数
|
up