Copy文件描述符

复制文件描述符可以用Linux系统提供的两个系统调用 dup(), dup2(),复制后,新的描述符也标识旧描述符所标识的文件。这样的话,原来的文件描述符和新的文件描述符都指向同一个文件,我们操作这两个文件描述符的任何一个,都能操作它所对应的文件。就像配钥匙一样,新钥匙和旧钥匙都能打开原来的那把锁。(这个类比是从网上看到的,感觉相当形象。)

dup()

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

int main()
{
    int file1, file2;
    file1 = open("./main.new",O_WRONLY|O_CREAT,0666);
    if(file1 < 0)
    {
        perror("open file");
        return -1;
    }
    printf("旧 的文件描述符 %d\n",file1);

    file2 = dup(file1);
    printf("新 的文件描述符 %d\n",file2);

    const char* buf1 = "我是 旧 描述符写进来的。\n";
    write(file1,buf1,strlen(buf1));

    const char* buf2 = "我是 新 描述符写进来的。\n";
    write(file2,buf2,strlen(buf2));

    close(file1);
    close(file2);
    return 0;
}
[lingyun@manjaro study]$ gcc study.c 
[lingyun@manjaro study]$ ./a.out
旧 的文件描述符 3
新 的文件描述符 12
[lingyun@manjaro study]$ cat main.new 
我是 旧 描述符写进来的。
我是 新 描述符写进来的。

dup2()

dup2() 和 dup() 的功能差不多,但是 dup2() 可以指定任意一个合法的数字作为新的文件描述符。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

int main()
{
    int file1, file2;
    file1 = open("./main.new",O_WRONLY|O_CREAT,0666);
    if(file1 < 0)
    {
        perror("open file");
        return -1;
    }
    printf("旧 的文件描述符 %d\n",file1);

    /*直接指定 1 为新的文件描述符,此时标准输出描述符就指向了文件 main.new,
    * 所以当执行 printf() 函数时,其内容会被打印到文件 main.new 中。
    * 相当于执行了
    * close(1);
    * file2 = dup(file1);
    * 这两条语句 */
    file2 = dup2(file1,1);
    printf("新 的文件描述符 %d\n",file2);

    const char* buf1 = "我是 旧 描述符写进来的。\n";
    write(file1,buf1,strlen(buf1));

    const char* buf2 = "我是 新 描述符写进来的。\n";
    write(file2,buf2,strlen(buf2));

    close(file1);
    close(file2);
    return 0;
}
[lingyun@manjaro study]$ gcc study.c 
[lingyun@manjaro study]$ ./a.out
旧 的文件描述符 3
[lingyun@manjaro study]$ cat main.new 
新 的文件描述符 1
我是 旧 描述符写进来的。
我是 新 描述符写进来的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Linux 系统中,没有名为 `copy` 的系统调用,但可以通过组合使用其他系统调用来实现文件复制。具体实现过程如下: 1. 打开原文件,获取文件描述符。 2. 创建目标文件,获取文件描述符。 3. 循环从原文件读取数据,写入目标文件,直到原文件读取结束。 4. 关闭原文件和目标文件。 具体实现可以参考下面的代码: ```c #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define BUF_SIZE 4096 int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s srcfile dstfile\n", argv[0]); exit(EXIT_FAILURE); } const char *srcfile = argv[1]; const char *dstfile = argv[2]; int srcfd = open(srcfile, O_RDONLY); if (srcfd == -1) { fprintf(stderr, "Failed to open %s: %s\n", srcfile, strerror(errno)); exit(EXIT_FAILURE); } int dstfd = open(dstfile, O_WRONLY | O_CREAT | O_EXCL, 0644); if (dstfd == -1) { fprintf(stderr, "Failed to create %s: %s\n", dstfile, strerror(errno)); close(srcfd); exit(EXIT_FAILURE); } char buf[BUF_SIZE]; ssize_t nread; while ((nread = read(srcfd, buf, BUF_SIZE)) > 0) { ssize_t nwritten = write(dstfd, buf, nread); if (nwritten != nread) { fprintf(stderr, "Failed to write data to %s: %s\n", dstfile, strerror(errno)); close(srcfd); close(dstfd); exit(EXIT_FAILURE); } } if (nread == -1) { fprintf(stderr, "Failed to read data from %s: %s\n", srcfile, strerror(errno)); close(srcfd); close(dstfd); exit(EXIT_FAILURE); } close(srcfd); close(dstfd); printf("Successfully copied %s to %s\n", srcfile, dstfile); exit(EXIT_SUCCESS); } ``` 注意,在上述实现中,使用了 `open`、`read` 和 `write` 等系统调用。使用 `open` 系统调用需要注意文件的访问权限问题。使用 `read` 和 `write` 系统调用需要注意数据读写的正确性和完整性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值