Linux的IO函数3

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值