inux ,编写一个程序,把一个文件内容剪切到另一个文件上

该问题来自社区帖: https://bbs.csdn.net/topics/616233367.为符合问答规范, 该问题经过ChatGPT优化

我需要在Linux上编写一个程序,实现将一个文件的内容剪切到另一个文件上的功能,类似于mv命令。要求只能使用open()、read()、write()和close()系统调用。

这是一个比较基础的文件操作问题,可以通过以下步骤实现:

  1. 使用 open() 系统调用打开源文件和目标文件。

  2. 使用 read() 系统调用从源文件读取数据(可以创建一个缓冲区来存储要读取的数据),然后使用 write() 系统调用将数据写入目标文件。

  3. 重复执行步骤2,直到源文件中的所有数据都已复制到目标文件中。

  4. 使用 close() 系统调用关闭文件。

以下是一个参考代码示例(仅作参考,请根据您的具体需求进行调整):

#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[]) {
    if (argc != 3) {
        write(STDERR_FILENO, "Usage: ./mycp <source_file> <destination_file>\n", 47);
        return 1;
    }

    int fd_src = open(argv[1], O_RDONLY);
    if (fd_src < 0) {
        write(STDERR_FILENO, "Failed to open source file.\n", 28);
        return 1;
    }
    
    int fd_dst = open(argv[2], O_CREAT | O_WRONLY, 0644);
    if (fd_dst < 0) {
        write(STDERR_FILENO, "Failed to open destination file.\n", 33);
        return 1;
    }

    char buffer[1024];
    ssize_t num_read = 0;
    ssize_t num_written = 0;
    while ((num_read = read(fd_src, buffer, sizeof(buffer))) > 0) {
        num_written = 0;
        while (num_written < num_read) {
            ssize_t res = write(fd_dst, &buffer[num_written], num_read - num_written);
            if (res < 0) {
                write(STDERR_FILENO, "Failed to write to destination file.\n", 37);
                close(fd_src);
                close(fd_dst);
                return 1;
            }
            num_written += res;
        }
    }

    close(fd_src);
    close(fd_dst);

    return 0;
}
```bash
#!/bin/bash

# 检查参数数量
if [ $# -ne 2 ]; then
  echo "Usage: $0 <source_file> <destination_file>"
  exit 1
fi

# 检查源文件是否存在
if [ ! -f "$1" ]; then
  echo "Error: Source file '$1' does not exist"
  exit 1
fi

# 打开源文件和目标文件
exec 3< "$1"
exec 4> "$2"

# 读取源文件的内容并写入目标文件
while read -r -n 1 char <&3; do
  echo -n "$char" >&4
done

# 关闭文件描述符
exec 3<&-
exec 4>&-

# 删除源文件
rm "$1"

```

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    我可以给您提供以下实现方案:

    1. 首先,使用open()函数打开需要进行剪切操作的原文件和目标文件。需要注意的是,打开文件时需要传递正确的文件权限和标志位参数。

    2. 使用read()函数读取原文件的数据,并使用write()函数将数据写入目标文件中。这里需要注意的是,在写入操作之前,需要将目标文件的文件指针移动到文件末尾,以便将数据追加到文件的末尾。此外,写入操作后需要用ftruncate()函数截短原文件长度,使其内容与目标文件对应。

    3. 最后,使用close()函数关闭文件描述符,释放系统资源。

    下面是具体实现代码:

    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        if (argc != 3) {
            printf("Usage: %s <source_file> <target_file>\n", argv[0]);
            return 1;
        }
    
        int src_fd = open(argv[1], O_RDWR);
        if (src_fd < 0) {
            perror("open source file error");
            return 1;
        }
    
        int dst_fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0777);
        if (dst_fd < 0) {
            close(src_fd);
            perror("open target file error");
            return 1;
        }
    
        off_t off = lseek(dst_fd, 0, SEEK_END);
        if (off < 0) {
            perror("lseek error");
            close(src_fd);
            close(dst_fd);
            return 1;
        }
    
        char buf[1024];
        ssize_t nread = 0;
        ssize_t nwrite = 0;
        while ((nread = read(src_fd, buf, sizeof(buf))) > 0) {
            nwrite = write(dst_fd, buf, nread);
            if (nwrite != nread) {
                perror("write error");
                close(src_fd);
                close(dst_fd);
                return 1;
            }
        }
    
        if (nread < 0) {
            perror("read error");
            close(src_fd);
            close(dst_fd);
            return 1;
        }
    
        if (ftruncate(src_fd, 0) == -1) {
            perror("ftruncate error");
            close(src_fd);
            close(dst_fd);
            return 1;
        }
    
        close(src_fd);
        close(dst_fd);
        return 0;
    }
    

    需要注意的是,在实现过程中需要对每个系统调用的返回值进行检查并进行错误处理。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^