# include <unistd.h>
int chdir ( const char * path) ;
作用:修改进程的工作目录
比如在/ home/ nowcoder 启动了一个可执行程序a. out, 进程的工作目录 / home/ nowcoder
参数:
path : 需要修改的工作目录
# include <unistd.h>
char * getcwd ( char * buf, size_t size) ;
作用:获取当前工作目录
参数:
- buf : 存储的路径,指向的是一个数组(传出参数)
- size: 数组的大小
返回值:
返回的指向的一块内存,这个数据就是第一个参数
* /
# include <unistd.h>
# include <stdio.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <fcntl.h>
int main ( ) {
char buf[ 128 ] ;
getcwd ( buf, sizeof ( buf) ) ;
printf ( "当前的工作目录是:%s\n" , buf) ;
int ret = chdir ( "/home/nowcoder/Linux/lesson13" ) ;
if ( ret == - 1 ) {
perror ( "chdir" ) ;
return - 1 ;
}
int fd = open ( "chdir.txt" , O_CREAT | O_RDWR, 0664 ) ;
if ( fd == - 1 ) {
perror ( "open" ) ;
return - 1 ;
}
close ( fd) ;
char buf1[ 128 ] ;
getcwd ( buf1, sizeof ( buf1) ) ;
printf ( "当前的工作目录是:%s\n" , buf1) ;
return 0 ;
}
# include <sys/stat.h>
# include <sys/types.h>
int mkdir ( const char * pathname, mode_t mode) ;
作用:创建一个目录
参数:
pathname: 创建的目录的路径
mode: 权限,八进制的数
返回值:
成功返回0 , 失败返回- 1
* /
# include <sys/stat.h>
# include <sys/types.h>
# include <stdio.h>
int main ( ) {
int ret = mkdir ( "aaa" , 0777 ) ;
if ( ret == - 1 ) {
perror ( "mkdir" ) ;
return - 1 ;
}
return 0 ;
}
# include <stdio.h>
int rename ( const char * oldpath, const char * newpath) ;
* /
# include <stdio.h>
int main ( ) {
int ret = rename ( "aaa" , "bbb" ) ;
if ( ret == - 1 ) {
perror ( "rename" ) ;
return - 1 ;
}
return 0 ;
}