1.目录操作函数
/*
#include <unistd.h>
int chdir(const char *path);
作用:修改进程的工作目录
比如在/home/kekecoder启动了一个可执行程序,a.out,进程的工作目录/home/kekecoder
参数:
Path:需要修改的工作目录
#include <unistd.h>
char *getcwd(char *buf, size_t size);
作用:获取当前工作目录
参数:
-buf:保存到buf里面,存储的路径,指向的是一个数组
-size:数组的大小
返回值:
返回的指向的一块内存,这个数据就是第一个参数
kekecoder@kekecoder-virtual-machine:~/Linux/lesson14$ ./chdir
当前的工作目录是:/home/kekecoder/Linux/lesson14
当前的工作目录是:/home/kekecoder/Linux/lesson13
#include<stdio.h>
#include<unistd.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/kekecoder/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;
}