进程2月29日

题目:要求将当前路径下,所有文件的权限及最后一次的访问时间提取出来,写入到file.txt中。(提示:opendir readir stat -->提取出来的数据写入到file.txt中)

代码:

cpp 复制代码
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>

int main(int argc, const char *argv[])
{
    //打开一个文件夹
    DIR* dp = opendir("./");
    if(NULL == dp)
    {
        perror("opendir");
        return -1;
    }

    //打开file.txt存储获取到的权限和时间
    FILE* fp = fopen("./fileno.txt","w");
    if(NULL == fp)
    {
        perror("fopen");
        return -1;
    }

    //读取文件夹                                                                  
    struct dirent* rp =NULL;
    struct stat buf;
    struct tm* info;
    while(1)
    {
        rp = readdir(dp);
        if(NULL ==rp)
        {
            break;
        }

        if(stat(rp->d_name,&buf) < 0)
        {
            perror("stat");
            return -1;
        }

        //提取文件权限
        mode_t m = buf.st_mode & 0777;
        //提取文件最后一次访问的时间st_atime
        info = localtime(&buf.st_atime);

        //将获取到的数据写入到file.txt中
        fprintf(fp,"0%o %d-%02d-%02d %02d:%02d:%02d %s\n",\
                m,info->tm_year+1900,info->tm_mon+1,info->tm_mday,\
                info->tm_hour,info->tm_min,info->tm_sec,rp->d_name);
    }

    closedir(dp);
    fclose(fp);

    return 0;
}

运行结果:

题目:

使用文件IO对图片进行拷贝。要求子进程拷贝后半部分,父进程拷贝前半部分。

提示:

1.图片就是一个普通文件,普通文件怎么拷贝,图片就怎么拷贝。读一个字符,写一个字符,循环size/2次

2.diff可以查看两个文件是否一致,例如diff 1.png 2.png,可以查看两张图片是否一致

3.终端输入:eog 1.png 可以打开1.png这张图片

代码:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

 int main(int argc, const char *argv[])
 {
    int source_fd, dest1_fd, dest2_fd;
    off_t size;
    char buffer;
    pid_t pid;

    // 打开源文件和目标文件  
    source_fd = open("./photo/QQ图片20221104192231.jpg", O_RDONLY);                                                                                 
    if (source_fd == -1) {
        perror("Error opening source file");
        exit(EXIT_FAILURE);
    }

    dest1_fd = open("dest1.png", O_WRONLY | O_CREAT, 0666);
    if (dest1_fd == -1) {
        perror("Error opening destination file 1");
        exit(EXIT_FAILURE);
    }

    dest2_fd = open("dest2.png", O_WRONLY | O_CREAT, 0666);
    if (dest2_fd == -1) {
        perror("Error opening destination file 2");
        exit(EXIT_FAILURE);
    }

    // 获取文件大小  
    if (lseek(source_fd, 0, SEEK_END) == -1) {
        perror("Error seeking to end of file");
        exit(EXIT_FAILURE);
    }
    size = lseek(source_fd, 0, SEEK_SET);
    if (size == -1) {
        perror("Error getting file size");
        exit(EXIT_FAILURE);
    }

    // 创建子进程  
    pid = fork();
    if (pid == -1) {
        perror("Error forking");
        exit(EXIT_FAILURE);
    }

    // 父进程拷贝前半部分  
    if (pid > 0) {
        // 回到文件开头  
        lseek(source_fd, 0, SEEK_SET);
        lseek(dest1_fd, 0, SEEK_SET);

        // 拷贝size/2个字节到dest1_fd  
        for (off_t i = 0; i < size / 2; ++i) {
            if (read(source_fd, &buffer, 1) != 1) {
                perror("Error reading from source file");
                exit(EXIT_FAILURE);
            }
            if (write(dest1_fd, &buffer, 1) != 1) {
                perror("Error writing to destination file 1");
                exit(EXIT_FAILURE);
            }
        }
    }
    // 子进程拷贝后半部分  
    else if (pid == 0) {
        // 跳到文件的后半部分  
        lseek(source_fd, size / 2, SEEK_SET);
        lseek(dest2_fd, 0, SEEK_SET);

        // 拷贝size/2个字节到dest2_fd  
        for (off_t i = 0; i < size / 2; ++i) {
            if (read(source_fd, &buffer, 1) != 1) {
                perror("Error reading from source file");
                exit(EXIT_FAILURE);
            }
            if (write(dest2_fd, &buffer, 1) != 1) {
                perror("Error writing to destination file 2");
                exit(EXIT_FAILURE);
            }
        }

        // 子进程结束  
        exit(EXIT_SUCCESS);
    }

    // 关闭文件描述符  
    close(source_fd);
    close(dest1_fd);
    close(dest2_fd);

    // 父进程等待子进程结束  
    wait(NULL);

    printf("Files copied successfully.\n");
    return 0;
}
相关推荐
程序媛小盐14 分钟前
贪心算法:最小生成树
算法·贪心算法·图论
Panesle16 分钟前
分布式异步强化学习框架训练32B大模型:INTELLECT-2
人工智能·分布式·深度学习·算法·大模型
多多*32 分钟前
算法竞赛相关 Java 二分模版
java·开发语言·数据结构·数据库·sql·算法·oracle
逐光沧海33 分钟前
数据结构基础--蓝桥杯备考
数据结构·c++·算法·蓝桥杯
Kidddddult34 分钟前
力扣刷题Day 48:盛最多水的容器(283)
算法·leetcode·力扣
AndrewHZ1 小时前
【Python生活】如何构建一个跌倒检测的算法?
python·算法·生活·可视化分析·陀螺仪·加速度计·跌倒检测
写个博客1 小时前
代码随想录算法训练营第三十九天
算法
源码方舟2 小时前
【基于ALS模型的教育视频推荐系统(Java实现)】
java·python·算法·音视频
fancy1661663 小时前
力扣top100 矩阵置零
人工智能·算法·矩阵
元亓亓亓3 小时前
LeetCode热题100--240.搜索二维矩阵--中等
算法·leetcode·矩阵