zoukankan      html  css  js  c++  java
  • 获取当前目录getcwd,设置工作目录chdir,获取目录信息

    #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
    ", 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
    ", 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;
    }
  • 相关阅读:
    ajax 上传文件
    在linux服务器centos上使用svn同步代码到项目中
    css3 选择器 权重问题 (第二部分)
    css3 选择器 权重问题 (第一部分)
    css3 文本模型
    (java)剑指offer题三
    (java)剑指0ffer题二
    (java)剑指offer题一
    java程序入口main()方法浅析
    jar命令浅析
  • 原文地址:https://www.cnblogs.com/zhangxuan/p/6340439.html
Copyright © 2011-2022 走看看