进程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;
}
相关推荐
呼啦啦啦啦啦啦啦啦2 小时前
常见的排序算法
java·算法·排序算法
胡萝卜3.03 小时前
数据结构初阶:排序算法(一)插入排序、选择排序
数据结构·笔记·学习·算法·排序算法·学习方法
地平线开发者3 小时前
LLM 中 token 简介与 bert 实操解读
算法·自动驾驶
scx201310044 小时前
20250814 最小生成树和重构树总结
c++·算法·最小生成树·重构树
阿巴~阿巴~4 小时前
冒泡排序算法
c语言·开发语言·算法·排序算法
散1124 小时前
01数据结构-交换排序
数据结构·算法
yzx9910134 小时前
Yolov模型的演变
人工智能·算法·yolo
weixin_307779135 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
无聊的小坏坏5 小时前
拓扑排序详解:从力扣 207 题看有向图环检测
算法·leetcode·图论·拓扑学
wwww.bo6 小时前
机器学习(决策树)
算法·决策树·机器学习