进程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;
}
相关推荐
github_czy21 分钟前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁1 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子1 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z1 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao2 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx2 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背2 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
满分观察网友z2 小时前
别怕树!一层一层剥开它的心:用BFS/DFS优雅计算层平均值(637. 二叉树的层平均值)
算法
杰克尼3 小时前
1. 两数之和 (leetcode)
数据结构·算法·leetcode
YuTaoShao4 小时前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展