20240808文件IO

编写代码实现shell命令CP,CAT,DIFF功能

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

int main(int argc, char** argv) {
    if (argc != 3) {
        printf("输入不合法(格式: my_cp 文件1 文件2)\n");
        return 1;
    }

    int fd1, fd2;
    fd1 = open(argv[1], O_RDONLY);
    if (fd1 < 0) {
        printf("打开文件%s失败\n错误码:%s\n", argv[1], strerror(errno));
        return 1;
    }

    fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (fd2 < 0) {
        printf("打开文件%s失败\n错误码:%s\n", argv[2], strerror(errno));
        close(fd1); // 确保关闭已打开的文件
        return 1;
    }

    ssize_t len;
    char buf[4096]; // 使用较大的缓冲区
    while ((len = read(fd1, buf, sizeof(buf))) > 0) {
        if (write(fd2, buf, len) != len) {
            printf("写入文件失败\n错误码:%s\n", strerror(errno));
            close(fd1);
            close(fd2);
            return 1;
        }
    }

    if (len < 0) {
        printf("读取文件失败\n错误码:%s\n", strerror(errno));
    }

    close(fd1);
    close(fd2);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define BUFFER_SIZE 4096

void print_file_content(const char *filename) {
    int fd = open(filename, O_RDONLY); // 打开文件以只读模式
    if (fd < 0) {
        fprintf(stderr, "无法打开文件 %s: %s\n", filename, strerror(errno));
        return;
    }

    char buffer[BUFFER_SIZE];
    ssize_t bytesRead;

    // 读取文件内容并写入标准输出
    while ((bytesRead = read(fd, buffer, sizeof(buffer))) > 0) {
        if (write(STDOUT_FILENO, buffer, bytesRead) != bytesRead) {
            fprintf(stderr, "写入标准输出失败: %s\n", strerror(errno));
            close(fd);
            return;
        }
    }

    if (bytesRead < 0) {
        fprintf(stderr, "读取文件失败: %s\n", strerror(errno));
    }

    close(fd);
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr, "用法: %s 文件1 [文件2 ...]\n", argv[0]);
        return 1;
    }

    // 逐个处理所有文件
    for (int i = 1; i < argc; ++i) {
        print_file_content(argv[i]);
    }

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define BUFFER_SIZE 1024

void print_diff(const char* file1, const char* file2) {
    int fd1 = open(file1, O_RDONLY);
    int fd2 = open(file2, O_RDONLY);

    if (fd1 < 0) {
        fprintf(stderr, "无法打开文件 %s: %s\n", file1, strerror(errno));
        exit(1);
    }

    if (fd2 < 0) {
        fprintf(stderr, "无法打开文件 %s: %s\n", file2, strerror(errno));
        close(fd1);
        exit(1);
    }

    char buffer1[BUFFER_SIZE];
    char buffer2[BUFFER_SIZE];
    ssize_t bytesRead1, bytesRead2;

    off_t offset1 = 0, offset2 = 0;
    int line_num = 1;
    int diff_found = 0;
    while ((bytesRead1 = read(fd1, buffer1, sizeof(buffer1))) > 0 &&
        (bytesRead2 = read(fd2, buffer2, sizeof(buffer2))) > 0) {

        if (bytesRead1 != bytesRead2 || memcmp(buffer1, buffer2, bytesRead1) != 0) {
            printf("文件 %s 和 %s 在第 %d 行有不同:\n", file1, file2, line_num);
            printf("文件 %s 内容:\n", file1);
            write(STDOUT_FILENO, buffer1, bytesRead1);
            printf("文件 %s 内容:\n", file2);
            write(STDOUT_FILENO, buffer2, bytesRead2);
            printf("\n");
            diff_found = 1;
        }
        line_num++;
    }

    if (bytesRead1 < 0) {
        fprintf(stderr, "读取文件 %s 失败: %s\n", file1, strerror(errno));
    }

    if (bytesRead2 < 0) {
        fprintf(stderr, "读取文件 %s 失败: %s\n", file2, strerror(errno));
    }
    if (diff_found == 0) {
        printf("文件 %s 和 %s 内容相同\n", file1, file2);
    }
    close(fd1);
    close(fd2);
}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        fprintf(stderr, "用法: %s 文件1 文件2\n", argv[0]);
        exit(1);
    }

    print_diff(argv[1], argv[2]);

    return 0;
}
 

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值