获取当前目录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\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;
}
时间: 2024-08-09 19:53:52

获取当前目录getcwd,设置工作目录chdir,获取目录信息的相关文章

PHP获取当前目录和相对目录的方法

// 获取当前目录echo realpath("."). "<br>"; echo getcwd() . "<br>"; // 获取上级目录echo realpath(".."). "<br>"; // 获取网站根目录echo $_SERVER['DOCUMENT_ROOT'];// 获取目录信息$path_parts = pathinfo(__FILE__); echo

python 获取当前目录,上级目录,上上级目录

1 import os 2 3 print '***获取当前目录***' 4 print os.getcwd() 5 print os.path.abspath(os.path.dirname(__file__)) 6 7 print '***获取上级目录***' 8 print os.path.abspath(os.path.dirname(os.path.dirname(__file__))) 9 print os.path.abspath(os.path.dirname(os.getcwd

Linux Kernel中获取当前目录方法(undone)

目录 0. 引言 1. 基于进程内存镜像信息struct mm_struct获取struct path调用d_path()获取当前进程的"绝对路径" 2. 基于文件描述符(fd).task_struct调用d_path()获取当前进程的"当前目录" 3. 基于dentry.vfsmount调用d_path()获取当前进程的"当前目录" 4. 基于get_fs_pwd获取当前目录 0. 引言 本文涉及的是ring0下的获取当前进程工作目录的方法,L

警惕System.Environment.CurrentDirectory 获取当前目录

最近工作中,要做个客户端提醒的小工具:winform程序自然少不了要读取和应用程序同一个目录的配置文件(不是exe.config文件): 要读取当前应用程序所在目录我立马想到了System.Environment.CurrentDirectory 来获取当前工作目录.程序运行似乎一切正常完美无缺: 到了第二天早上来上班一开机弹出:"读取配置文件丢失".应用程序增加了开机自启动.弹出这个消息读取配文件失败,立马检查应用程序目录下的配置文件是不是被自己删了.一检查发现文件还在呀,退出程序重

获取当前目录中的文件个数

一.获取当前目录下的文件个数 1.命令 ls -l | grep "^-" | wc -l 2.说明 ls -l 长列表输出该目录下文件信息(注意这里的文件,不同于一般的文件,可能是目录.链接.设备文件等):grep “^-“ 这里将长列表输出信息过滤一部分,只保留一般文件,如果只保留目录就是^d:wc -l 统计输出信息的行数,因为已经过滤得只剩一般文件了,所以统计结果就是一般文件信息的行数,又由于一行信息对应一个文件,所以也就是文件的个数 要想连隐藏文件一起统计,使用如下命令 ls

Python下获取当前目录中的所有子目录

p是输入目录 代码如下 import osdef getDirList(p):  #      b = [];    filepath=p    if filepath=="":        return b    filepath = filepath.replace( "/","\\")    if filepath[ -1] != "\\":        filepath = filepath+"\\&qu

cookies如何成为全局变量以及设置,删除,获取

(一)cookie机制将信息存储于用户硬盘,因此可以作为全局变量 (1)保存用户登录状态.例如将用户id存储于一个cookie内,这样当用户下次访问该页面时就不需要重新登录了,现在很多论坛和社区都提供这样的功能. cookie还可以设置过期时间,当超过时间期限后,cookie就会自动消失.因此,系统往往可以提示用户保持登录状态的时间:常见选项有一个月.三个 月.一年等. (2)跟踪用户行为.例如一个天气预报网站,能够根据用户选择的地区显示当地的天气情况.如果每次都需要选择所在地是烦琐的,当利用了

Swift - 常用文件目录路径获取(Home目录,文档目录,缓存目录等)

当前位置: 首页 > 编程社区 > Swift > Swift - 常用文件目录路径获取(Home目录,文档目录,缓存目录等) Swift - 常用文件目录路径获取(Home目录,文档目录,缓存目录等) 2015-06-15 16:06 发布:yuhang 浏览:207 iOS应用程序只能在自己的目录下进行文件的操作,不可以访问其他的存储空间,此区域被称为沙盒.下面介绍常用的程序文件夹目录: 1,Home目录  ./ 整个应用程序各文档所在的目录 1 2 //获取程序的Home目录 le

沙盒目录常用获取方式

获取程序的Home目录 NSString *home = NSHomeDirectory(); 注意: 虚拟机Home目录: /Users/userName/Library/Application Support/iPhone Simulator/6.1/Applications/C926CCFA-A110-45E2-B4AD-7E5679152D99 真机Home目录: /var/mobile/Applications/304D8530-98CB-4043-A208-EBF06E8ADBB9