转自:
http://blog.chinaunix.net/u2/63316/showart_547992.html
Linux下的目录是依照标准来实作的,因此,您可以毫无问题地移殖到任何其它UNIX平台。
--------------------------------------------------------------------------------
getcwd/getwd : 取得目前所在目录
--------------------------------------------------------------------------------
#include
char * getcwd(char *buf,size_t size);
buf将会返回目前路径名称。
任何的错误发生,将会返回NULL。如果路径长度超过size,errno为ERANGE。getcwd返回的值永远是没有symbol link的。
--------------------------------------------------------------------------------
#include
char *getwd(char *buf);
getwd是个危险的函数,一般都会强烈建议不要用,因为您无法确定最长的目录长度为多少。PATH_MAX定义了最长的路径长度。在Linux下所以提供这个函数主要是因为「传统」。
--------------------------------------------------------------------------------
//获取系统目录最大长度
long pathconf(char* path, int flag);
--------------------------------------------------------------------------------
chdir/fchdir/chroot : 改变目前所在目录
--------------------------------------------------------------------------------
#include
int chdir(const char * pathname);
int fchdir(int fd);
chdir根据pathname变更目前的所在目录,它只改变该程式的所在目录。
fchdir根据已开启的fd(file descriptor)目录来变更。
//sample
/*更改当前工作目录到上级目录*/
if(chdir("..")==-1){
perror("Couldn't change current working directory./n");
return 1;
}
--------------------------------------------------------------------------------
#include
int chroot(const char * path);
chroot改变该程式的根目录所在。例如chroot("/home/ftp")会将根目录换到/home/ftp下,而所有档案操作都不会超出这个围内。为保障安全性,当chdir("/..")时,将会仅切换到chdir("/"),如此便不会有档案安全问题。
--------------------------------------------------------------------------------
mkdir/rmdir : 造/移除目录
--------------------------------------------------------------------------------
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char * dirname,mode_t mode);
mkdir会造一个新目录出来,例如mkdir("/home/foxman",0755);。
如果该目录或档案已经存在,则操作失败。
/*mode 设置为0700,开始的0表示八进制*/
if(mkdir("/home/zxc/z", 0700) == -1){
perror("Couldn't create the directory./n");
return 1;
}
--------------------------------------------------------------------------------
#include <unistd.h>
int rmdir(char * pathname);
这个函数移除pathname目录。
--------------------------------------------------------------------------------
//获得文件信息
#include <sys/types.h> <sys/stat.h> <unistd.h>
int stat(const char* path, struct stat* buf);
int fstat(int filedes, struct stat* buf);
int lstat(const char* path, struct stat* buf);
opendir/readdir/closedir/rewinddir : 读取目录资讯
--------------------------------------------------------------------------------
#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则可重新读取目录资讯。
以下是个标准例。
--------------------------------------------------------------------------------
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;
}
c语言实现目录遍历
这样我们就可以对整个目录进行遍历搜索,并输出显示其完整的文件路径。以上的程序在Visual C++ 6.0中已调试通过。
#include <dirent.h>
#include <stdio.h>
int main()
{
DIR *dir;
struct dirent *subfile;
dir = opendir(".");
while ((subfile = readdir(dir)) != NULL) {
printf("%s/n", subfile->d_name);
}
return 0;
}
UNIX: lstat,readdir,opendir
win32: findfirst,findnext
这个头文件中的函数只不过提供了简单的文件目录服务而已
使用文件目录服务和学操作系统编程是两码事
你要使用文件目录服务根本不需要从新编写文件系统, 也不必要换操作系统
因为无论哪个操作系统都会提供文件目录编程接口
windows 下需要文件目录服务时一般是使用 Win API ( 当然,也可以使用 文件系统 API 的某个封装 ). 简单的文件目录服务可以用 C 运行时库函数
windows 下和 文件目录服务 有关的 C 运行时库函数 的声明 一般 在 direct.h 和 io.h 中.比如 linux 平台下的 opendir 就可以用 dev c++ 的头文件 io.h 中的函数 chdir 与 direct.h 中的 _chdrive 替代.
如果想使用 Win API 来访问文件目录服务, 你应该先去找本 Windows 编程基础的书来看.