首页 > 其他 > 详细

获取当前目录getcwd,设置工作目录chdir,获取目录信息

时间:2017-01-22 16:43:02      阅读:203      评论:0      收藏:0      [点我收藏+]
#include <unistd.h>
#include <stdio.h>
#include <limits.h>


int main(int argc, char* argv[])
{
    char buf[PATH_MAX];
    
    getcwd(buf, PATH_MAX-1);
    
    printf("the current path is :%s\n", buf);

    return 0;
}

设置工作目录:

#include <unistd.h>

int chdir(const char *path);
int fchdir(int fd);

chdir() changes the current working directory of the calling process to the directory specified in path.

fchdir() is identical to chdir(); the only difference is that the directory is given as an open file descriptor.

 -----------------------------------

只要对目录有读写权限,就可获取目录信息。

打开目录:opendir

读取目录: readdir

关闭目录:closedir

DIR *opendir(const char *name);
DIR *fdopendir(int fd);

struct dirent *readdir(DIR *dirp);

int closedir(DIR *dirp);

 

#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>


int my_readdir(const char* path)
{
    DIR *dirp;
    struct dirent *ptr;

    if ( (dirp=opendir(path)) == NULL)
    {
        return -1;
    }

    while( (ptr=readdir(dirp)) != NULL)
    {
        printf("file name is:%s\n", ptr->d_name);
    }
    
    return 0;
}


int main(int argc, char* argv[])
{
    
    if (argc < 2)
    {
        exit(0);
    }

    if(my_readdir(argv[1])  < 0)
    {
        exit(0);
    }

    return 0;
}

 

获取当前目录getcwd,设置工作目录chdir,获取目录信息

原文:http://www.cnblogs.com/zhangxuan/p/6340439.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!