-
linux --chdir() 改变当前工作目录函数
2012-05-14 11:39:03说明:chdir函数用于改变当前工作目录。调用参数是指向目录的指针,调用进程需要有搜索整个目录的权限。每个进程都具有一个当前工作目录。在解析相对目录引用时,该目录是搜索路径的开始之处。如果调用进程更改了...linux --目录chdir函数
int chdir(const char *path );
说明:chdir函数用于改变当前工作目录。调用参数是指向目录的指针,调用进程需要有搜索整个目录的权限。每个进程都具有一个当前工作目录。在解析相对目录引用时,该目录是搜索路径的开始之处。如果调用进程更改了目录,则它只对该进程有效,而不能影响调用它的那个进程。在退出程序时,shell还会返回开始时的那个工作目录。
(1) 内核解析参数中的路径名,并确保这个路径名有效。为了做到这一点,就路径名解析而言,内核使用相同的算法。如果路径名无效,它输出错误消息并退出。
(2) 如果路径名有效,内核定位该目录的索引节点,并检查它的文件类型和权限位,确保目标文件是目录以及进程的所有者可以访问该目录(否则改变到新目录就没有用)。
(3) 内核用新目标目录的路径名和/或索引节点替换u区中当前目录路径名和/或它的索引节点号。
错误信息:
EFAULT: path 指向了非法地址
ENAMETOOLNG:路径过长
ENOENT:文件不存在
ENOMEM:内核内存不足
ENOTDIR:给出路径不是目录
EACCES:无访问路径中某个目录的权限
ELOOP:解析路径中太多的符号链接
EIO:发生I/O错误
实例1:
#include <unistd.h>
#include <iostream>
int main(void)
{
long cur_path_len;
char* cur_work_dir;
if((cur_path_len = pathconf(".",_PC_PATH_MAX)) == -1)
{
perror("Couldn`t get currentworking path length");
return 1;
}
std::cout<<"Current path lengthis "<< cur_path_len<<std::endl;
if((cur_work_dir = (char*)malloc(cur_path_len)) == NULL)
{
perror("Couldn't allocate memoryfor the pathname");
return 1;
}
if (getcwd(cur_work_dir,cur_path_len)==NULL)
{
perror("Couldn`t get currentworking directory!");
}
else
{
std::cout<< "Currentworking directory is"<<cur_work_dir<<std::endl;
}
if(chdir("..") == -1)
{
perror("Couldn`t change current working diretory!");
return 1;
}
if ((getcwd(cur_work_dir,cur_path_len)) == NULL)
{
perror("Couldn`t get currentworking directory!");
return 1;
}
std::cout<<"Afterchangedirectory,Current working directory is"<<cur_work_dir<<std::endl;
free(cur_work_dir);
return 0;
}
-
第三十一节 Linux系统编程- Linux系统编程管理文件和目录-chdir改变当前目录(七)
2019-07-27 20:23:06在实际应用中,代码可能需要从当前目录中进到其它目录,这个时候首先需要使用 getcwd函数获取当前目录,保存起来,然后使用 chdir 跳到其它目录,完成操作,然后再使用 chdir回到最初保存的目录。 使用 man 学习 ...-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.
第一:
使用代码改变当前工作目录
在实际应用中,代码可能需要从当前目录中进到其它目录,这个时候首先需要使用 getcwd函数获取当前目录,保存起来,然后使用 chdir 跳到其它目录,完成操作,然后再使用 chdir回到最初保存的目录。
使用 man 学习 chdir 函数
1)如下图所示,使用命令“man 2 chdir”
2)如下图所示,有两个类似的函数 chdir,fchdir
3)接着注意一下相关的函数,如下图所示
4)接着介绍一下 chdir 和 fchdir 的用法
int chdir(const char *path);
参数 *path:文件路径
返回值:成功返回 0,错误返回-1。
int fchdir(int fd);
参数 fd:open 函数返回的句柄,文件描述符。
返回值:成功返回 0,错误返回-1。第二:
chdir 函数例程
编写简单的 chdir.c 文件测试 chdir 函数
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> //chdir和fchdir函数头文件 #include <unistd.h> #define LENTH 255 int main(int argc,char *argv[]) { int ret; char pwd[LENTH]; //检测参数 if(argc <3){ printf("\nPlease input file path\n"); return 1; } //getcwd函数获取当前目录 //getcwd 函数用于保存程序运行时候目录 if(!getcwd(pwd,LENTH)){ perror("getcwd"); return 1; } printf("\ngetcwd pwd is %s\n",pwd); //使用chdir函数转入其他目录 //使用 chdir 跳转到的目录“argv[1]” //在目录“argv[1]”下,做一个小操作,将目录“argv[2]”删除掉(前提是argv[1]目录下必须有 argv[2]目录,运行前新建一个即可) ret = chdir(argv[1]); if(ret){ printf("Please make sure file path\n"); return 1; } printf("chdir %s is success!\n",argv[1]); //转入其他目录,完成操作 //使用rmdir函数删除目录 ret = rmdir(argv[2]); if(ret<0){ printf("rmdir %s failed!\n",argv[2]); return 1; } printf("rmdir %s is success!\n",argv[2]); //再次使用chdir回到pwd保存的目录 //调用 chdir 函数,返回 pwd 保存的目录 ret = chdir(pwd); if(ret){ printf("Please make sure file path\n"); return 1; } printf("chdir %s is success!\n",pwd); return 0; }
第三:
编译运行测试
1)在 Ubuntu 系统下,如下图所示,进入前面实验创建的目录
“/home/linuxsystemcode/CatlogFile”,将源码 chdir.c 拷贝进去,进入文件夹 CatlogFile,如下图所示。2)使用命令“arm-none-linux-gnueabi-gcc -o chdir chdir.c -static”编译 chdir 文件,如下图所示,使用命令“ls”可以看到生成了 chdir 可执行文件
3)将文件拷贝到挂载点
4)运行程序前,使用使用命令“mkdir /mnt/test”在/mnt 目录下新建一个 test 目录,如下图所示,“/mnt”目录将作为一个参数传递给 chdir 程序,另一个参数“/mnt/test”也会传递过去,这个“/mnt/test”目录将会被删除。
5)开发板挂载后运行文件
如上图所示,程序运行之后,再次使用 ls 命令查看,会发现“mnt”目录下的“test没有被删除。
疏忽了,在虚拟机建了个test目录,所以失败了。不过,在后面在开发板上建了test目录,验证代码没问题。
-
Linux目录访问函数汇总
2020-04-14 07:51:42Linux下目录访问函数总结,主要是涉及到的函数,以及所在头文件。 获得工作目录: #include <unistd.h> char *getcwd(char *buf,size_t size);char *getwd(char *buf);...改变当前目录: #include <un...Linux下目录访问函数总结,主要是涉及到的函数,以及所在头文件。
获得工作目录:
#include <unistd.h> char *getcwd(char *buf,size_t size);char *getwd(char *buf);/*this is for FreeBSD*/
改变当前目录:
#include <unistd.h> int chdir(const char *path);
保存当前目录:
#include <unistd.h> int fchdir(int fd);
建立新目录:
#include <sys/type.h> #include <sys/stat.h> int mkdir(const char *path,mode_t mode);
删除目录:
#include <unistd.h> int rmdir(const char* path);
打开目录进行搜索:
#include <sys/type.h> #include <dirent.h> DIR *opendir(const char *pathname); int dirfd(DIR *dirp);
关闭目录:
#include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp);
搜索目录:
#include <sys/type.h> #include <dirent.h> struct dirent *readdir(DIR *dirp);
####重新回到目录的开始:
#include <sys/type.h> #include <dirent.h> void rewinddir(DIR *dirp);
保存目录中的位置:
#include <sys/type.h> #include <dirent.h> long telldir(const DIR *dirp);
在目录内恢复位置:
#include <sys/type.h> #include <dirent.h> void seekdir(DIR *dirp,long loc);
扫描目录:
#include <sys/type.h> #include <dirent.h> int scandir(const char *diename,struct dirent ***namelist,int (*select)(struct dirent *),int (*compar)(const void *,const viod*));
遍历目录结构:
#include <ftw.h> int ftw(const char* path,int(*fn)(const char *obj_path,const struct stat *obj_stat,int obj_flags),int depth); int nftw(const char* path,int(*fn)(const char *obj_path,const struct stat *obj_stat,int obj_flags,struct FTW obj_FTW),int depth,int flags);
改变根目录:
#include <unistd.h> int chroot(const char *dirname);
喜欢请关注:
-
Linux目录操作函数
2019-09-20 15:47:54一、chdir函数:修改当前进程的路径 <unistd.h>,修改此函数所在进程的可执行程序的历经 int chdir(const char*path); ./a.out ../test/. 改变./a.out 进程所在的路径 二、getcwd函数:获取当前进程的工作...一、chdir函数:修改当前进程的路径 <unistd.h>,修改此函数所在进程的可执行程序的历经
int chdir(const char*path);./a.out ../test/. 改变./a.out 进程所在的路径
二、getcwd函数:获取当前进程的工作路径
getcwd(buf, sizeof(buf));三、mkdir函数 int mkdir(const char*str, mode_t mode); 此函数创建的目录需要给执行权限,否则打不开
四、rmdir函数:删除一个空目录
int rmdir(const char*pathname);属于man第三章
五、opendir函数:打开一个目录
DIR *opendir(const char *name);
DIR *fopendir(int fd);DIR结构指针,内部结构体,保存所打开目录的信息
六、readdir:读目录
struct dirent *readdir(DIR *ddirp); //返回一个记录文件信息的结构体****opendir和readdir应用——递归读取目录中文件个数
Linux目录结构就是树状结构,遍历目录需用到递归
#include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <stdlib.h> #include <string.h> int getFileNum(char *path) { // open dirent DIR *dir = NULL; dir = opendir(path); if(dir == NULL) { perror("opendir"); exit(1); } //遍历当前打开目录 struct dirent *ptr = NULL; char tmpPath[1024] = {0}; int total = 0; while((ptr = readdir(dir)) != NULL) { //过滤 . 和 .. if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) { continue; } if(ptr->d_type == DT_DIR) { //递归读目录 sprintf(tmpPath, "%s/%s", path, ptr->d_name); total += getFileNum(tmpPath); } //如果是普通文件 if(ptr->d_type == DT_REG) { total++; } } closedir(dir); //获取完毕必须关闭目录,否则获取不到数目 return total; } int main(int argc, char **argv) { if(argc < 2) { printf("./a.out dir\n"); exit(1); } int total = getFileNum(argv[1]); printf("%s has file numbers: %d\n", argv[1], total); return 0; }
七、文件描述符的复制(重定向)
dup和dup2函数:用来复制文件描述符
<unistd.h>描述符复制:新分配一个文件描述符使得两个文件描述符指向同一个文件
int dup(int oldfd); //返回文件描述符中未被使用的最小的描述符
int dup2(int olddup, int newfd);
1、old——》new:如果new是一个被打开的文件描述符,再拷贝前先关闭new
2、old和new对应同一个文件描述符,不会关闭new,直接返回old
fcntl:改变已经打开文件的属性,在文件打开时,不许关闭文件属性就可以改变文件读写权限
现阶段学会:获得/设置文件状态标记
int fcntl(int fd, int cmd,long arg);
arg:一般为0 -
关于Linux目录访问函数总结
2020-04-14 07:46:24Linux下目录访问函数总结,主要是涉及到的函数,以及所在头文件。 获得工作目录: #include <unistd.h> char *getcwd(char *buf,size_t size);char *getwd(char *buf);...改变当前目录: #inclu... -
chdir()改变当前工作目录 -- Linux
2015-09-02 11:05:35说明:chdir函数用于改变当前工作目录。调用参数是指向目录的指针,调用进程需要有搜索整个目录的权限 错误信息: EFAULT: path 指向了非法地址 ENAMETOOLNG:路径过长 ENOENT:文件不存在 ENOMEM:内核... -
python shell中python os模块实用函数(含改变当前工作路径、显示当前目录等)
2015-06-09 18:41:24在windows下使用python自带的gui shell来测试脚本,有时候我们需要进行如:切换/改变当前工作路径、显示当前目录、删除文件等。 所以,这些切换目录等操作都需要调用python的os 模块里的相关函数如下: os.sep可以... -
linux文件和目录管理常见的函数
2018-06-23 21:56:18pwd在终端查看路径3.mkdir 函数-使用代码新建目录4.rmdir 函数-使用代码删除目录5.chdir 函数-使用代码改变当前工作目录6.opendir 和 closedir 函数- 用于读取目录的内容,相当于命令中的 ls 命令6.readdir 函数-... -
Linux笔记(11)-Linux目录操作常用函数
2019-05-26 19:36:02重命名函数:rename 1. 函数原型: int rename(const char *oldPathName,const char *newPathName); 修改当前的工作目录:chdir 1. 函数原型: int chdir(const char *dirPath);...获取当前目录的路... -
Linux C/C++编程之(十五)目录操作相关函数
2020-07-04 10:16:59函数作用:获取当前目录 头文件 参数说明: buf传出参数,路径 size缓冲区大小 返回值 成功:返回路径的指针 失败:返回NULL 2. chdir 函数作用:改变工作路径 头文件 函数参数: path对应的目标工作路径 -
LINUX文件与目录笔记----chdir函数
2014-01-28 16:32:59LINUX文件与目录笔记----chdir函数 2011-08-15 14:47:49| 分类: linux | 标签:linux 笔记 |举报|字号 订阅 ...说明:chdir函数用于改变当前工作目录。调用参数是指向目录的指针 -
(15)Linux_C_文件及目录函数
2010-02-23 18:19:36access(判断是否具有存取文件的权限)alphasort(依字母顺序排序目录结构)chdir(改变当前的工作目录)chmod(改变文件的权限)chown(改变文件的所有者)chroot(改变根目录)closedir(关闭目录)fchdir(改变当前的工作目录)... -
linux目录操作
2020-06-22 05:47:47在介绍目录之前 我们想来介绍一下目录项 ...1.把当前进程的工作路劲改变到另一个路劲下面创建一个文件,注意:这个并没有改变shell的路劲,知识改变当前运行程序的工作路劲 int main(int argc, char*arg -
Linux C 目录操作
2021-02-08 15:17:41改变当前工作目录4. 创建和删除目录5. 目录的读取5.1 读取目录前,需要先打开目录;读取完毕后,需要关闭打开的目录流指针5.2 读取目录5.3 判断文件类型函数6. 实例用户在命令行给出目录,要求遍历所有子目录并判断... -
Linux之目录操作和内存映射
2020-04-30 15:53:07Linux之目录操作 获取当前目录 #include<unistd.h> char* getcwd(char* buf, size_t size); 函数getcwd把当前目录的绝对路径名复制到buf中,该缓冲有size个字长,如果装不下完整路径...改变当前目录 #include... -
linux系统目录操作
2018-04-03 15:14:481 chdir 函数。修改当前进程的路径 ... 函数说明:chdir()用来将当前的工作目录改变成以参数path 所指的目录. 2 getcwd 函数 获取当前进程工作目录 函数原型:char *getcwd(char *buf, size_t size); ... -
linux中C语言编程chdir函数
2019-09-22 06:00:17chdir函数改变目录后仅仅对当前进程有效,shell退出之后还是会回到原来的目录。 转载于:https://www.cnblogs.com/kirsten/p/3468376.html -
linux getcwd、chdir函数详解
2020-05-27 16:11:07getcwd函数: 头文件:unistd.h 函数原型:char *getcwd(char *buf, size_t size); 功能:getcwd()会将当前工作目录的绝对路径复制到...功能:改变当前工作目录 返回值:成功返回0,失败返回-1 示例程序getcwd_c. -
Linux更改获取进程工作目录
2020-11-07 23:44:33文章目录getcwd()获取当前工作目录函数的声明:普通的用法:高级用法chdir()改变进程工作目录函数的声明:使用:/home/deroy/Desktop/Linux高性能服务器编程/LinuxServerCodes/7 / #includegetcwd()获取当前工作目录... -
Linux下目录操作
2019-09-27 22:29:05目录相关函数介绍 //改变目录或文件的访问权限 #include<sys/stat.h> int chmod(const char* path,mode_t mode);//mode为八进制 //获取当前的工作路径 ...//获取当前目录,相当与pwd... -
unix/linux下一些常用函数
2015-06-09 22:34:361、目录操作 (1)mkdir("目录名",权限) 创建目录 (2)rmdir("目录名") 删除目录 (3)opendir("目录名") ...(6)chdir("目录名") 改变当前工作目录 (7)getcwd(字符数组名,字符数组长度) 取得当前工作目录 (8) -
(2.6)文件和目录操作——Linux目录操作2
2019-11-26 22:53:55改变当前工作目录:chdir4.设置目录读取位置:rewinddir5.设置目录读取位置:seekdir6.获取目录读取位置:telldir7.读取特定目录数据 1.创建目录:mkdir 头文件: sys/stat.h 函数声明: int mkdir(const char *... -
linux常用c函数 文件权限控制篇
2011-06-03 09:10:00access(判断是否具有存取文件的权限)alphasort(依字母顺序排序目录结构)chdir(改变当前的工作(目录)chmod(...改变当前的工作目录)fchmod(改变文件的权限)fchown(改变文件的所有者)fstat(由文件描述词取得 -
linux下的目录操作(未完成版)
2019-05-20 09:25:554.获取、改变当前目录 5.以后有的话再补充。 1.创建和删除目录 头文件 #include<sys/stat.h> #include<sys/types.h> #include<unistd.h> 函数 int mkdir(const char *pathname, ... -
Linux 常用C函数说明-文件权限控制篇
2012-02-09 08:22:59chdir(改变当前的工作(目录)相关函数 getcwd,chroot表头文件 #include<unistd.h>定义函数 int chdir(const char * path);函数说明 chdir()用来将当前的工作目录改变成以参数path所指的目录。... -
(2.4)文件和目录操作——Linux文件定位
2019-11-19 23:08:51lseek(lseek函数用于改变文件的当前偏移量。 ) 头文件 #include<unistd.h> 定义函数 off_t lseek(int filedes, off_t offset, int origin); /* 系统调用函数 将文件描述符的文件位置设定成指定的值; 只... -
linux编程学习笔记(十二) 遍历目录
2013-06-12 17:59:001 默认情况下 实际用户和有效用户是一样的 实际用户:执行用户 有效用户:权限用户 getuid() 实际用户 geteuid() 有效用户 ... chmod u+s 之后 ,其他人执行文件时,实际用户和有效...改变当前目录 ...