Linux实验4

#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 4096

void copy_file(const char *source_file, const char *dest_file) {
    FILE *source, *dest;
    char buffer[BUFFER_SIZE];
    size_t bytesRead;

    // 打开源文件以读取
    source = fopen(source_file, "rb");
    if (source == NULL) {
        perror("无法打开源文件");
        exit(EXIT_FAILURE);
    }

    // 打开目标文件以写入
    dest = fopen(dest_file, "wb");
    if (dest == NULL) {
        perror("无法创建目标文件");
        fclose(source);
        exit(EXIT_FAILURE);
    }

    // 从源文件读取内容并写入目标文件
    while ((bytesRead = fread(buffer, 1, sizeof(buffer), source)) > 0) {
        fwrite(buffer, 1, bytesRead, dest);
    }

    // 关闭文件
    fclose(source);
    fclose(dest);

    printf("文件复制成功: %s -> %s\n", source_file, dest_file);
}

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "用法: %s <源文件名> <目标文件名>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    const char *source_file = argv[1];
    const char *dest_file = argv[2];

    copy_file(source_file, dest_file);

    return 0;
}

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

void get_file_permissions(const char *filename) {
    struct stat file_stat;

    // 使用stat函数获取文件属性
    if (stat(filename, &file_stat) == -1) {
        perror("获取文件属性失败");
        return;
    }

    // 用户权限
    printf("用户权限: ");
    printf((file_stat.st_mode & S_IRUSR) ? "r" : "-");
    printf((file_stat.st_mode & S_IWUSR) ? "w" : "-");
    printf((file_stat.st_mode & S_IXUSR) ? "x" : "-");
    printf("\n");

    // 组权限
    printf("组权限: ");
    printf((file_stat.st_mode & S_IRGRP) ? "r" : "-");
    printf((file_stat.st_mode & S_IWGRP) ? "w" : "-");
    printf((file_stat.st_mode & S_IXGRP) ? "x" : "-");
    printf("\n");

    // 其他用户权限
    printf("其他用户权限: ");
    printf((file_stat.st_mode & S_IROTH) ? "r" : "-");
    printf((file_stat.st_mode & S_IWOTH) ? "w" : "-");
    printf((file_stat.st_mode & S_IXOTH) ? "x" : "-");
    printf("\n");
}

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

    const char *filename = argv[1];

    // 获取文件权限并输出结果
    get_file_permissions(filename);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值