系统编程中的文件描述符重定向和重命名dup/rename函数

系统编程中的文件描述符重定向和重命名dup/rename函数

思维导图

这里写图片描述

示例代码

  • rename 函数
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    if(argc < 3)
    {
        printf("a.out oldName newName\n");
        exit(1);
    }

    int ret = rename(argv[1], argv[2]);
    if(ret == -1)
    {
        perror("rename");
        exit(1);
    }
    return 0;
}
  • dup函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
    int fd = open("test.txt", O_RDWR);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }

    printf("file open fd = %d\n", fd);

    // 找到当前进程的文件描述符表中 ==第一个可用的文件描述符
    // 将参数指定的文件复制到该描述符后,返回这个描述符
    int ret = dup(fd);
    if(ret == -1)
    {
        perror("dup");
        exit(1);
    }
    printf("dup fd = %d\n", ret);
    char* buf = "你今天做大保健了吗????\n";
    char* buf1 = "你大爷的,我是个Gay!!!\n";
    write(fd, buf, strlen(buf));
    write(ret, buf1, strlen(buf1));

    close(fd);
    return 0;
}
  • dup2函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
    int fd = open("test.txt", O_RDWR);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }

    int fd1 = open("fuck.txt", O_RDWR);
    if(fd1 == -1)
    {
        perror("open");
        exit(1);
    }

    printf("fd = %d\n", fd);
    printf("fd1 = %d\n", fd1);

    int ret = dup2(fd1, fd);
    if(ret == -1)
    {
        perror("dup2");
        exit(1);
    }
    printf("current fd = %d\n", ret);
    char* buf = "那小子真帅 ^_^!!!!!!!!!!\n";
    write(fd, buf, strlen(buf));
    write(fd1, "迎娶白富美!", 12);

    close(fd);
    close(fd1);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux,标准输入(stdin)、标准输出(stdout)和标准错误(stderr)是预定义的文件描述符,它们分别是0、1和2。如果我们需要定向这些文件描述符,可以使用以下命令: ``` command < input_file.txt # 将文件input_file.txt的内容作为标准输入 command > output_file.txt # 将标准输出写入文件output_file.txt command 2> error_file.txt # 将标准错误输出写入文件error_file.txt ``` 如果需要恢复这些文件描述符到原始状态,我们可以使用dup2()函数。它可以将一个文件描述符复制到另一个文件描述符的位置上,从而实现定向的撤销。以下是一个示例代码: ```c #include <stdio.h> #include <unistd.h> #include <fcntl.h> int main() { int fd_in = open("input_file.txt", O_RDONLY); int fd_out = open("output_file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666); int fd_err = open("error_file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666); // 定向标准输入、输出和错误输出 dup2(fd_in, STDIN_FILENO); dup2(fd_out, STDOUT_FILENO); dup2(fd_err, STDERR_FILENO); // 执行一些需要定向的代码 printf("Hello, world!\n"); fprintf(stderr, "This is an error message.\n"); // 恢复标准输入、输出和错误输出 dup2(STDIN_FILENO, fd_in); dup2(STDOUT_FILENO, fd_out); dup2(STDERR_FILENO, fd_err); // 关闭文件描述符 close(fd_in); close(fd_out); close(fd_err); return 0; } ``` 在上面的代码,我们首先打开三个文件描述符,并将它们分别复制到标准输入、输出和错误输出的位置上。然后我们执行一些需要定向的代码,最后使用dup2()函数文件描述符恢复到原始状态。最后关闭文件描述符,释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值