进程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;
}
相关推荐
水蓝烟雨6 分钟前
[HOT 100] 2187. 完成旅途的最少时间
算法·hot 100
菜鸟一枚在这1 小时前
深度解析建造者模式:复杂对象构建的优雅之道
java·开发语言·算法
gyeolhada1 小时前
2025蓝桥杯JAVA编程题练习Day5
java·数据结构·算法·蓝桥杯
阿巴~阿巴~1 小时前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先
给bug两拳2 小时前
Day9 25/2/22 SAT
算法
_Itachi__2 小时前
LeetCode 热题 100 73. 矩阵置零
算法·leetcode·矩阵
夏末秋也凉3 小时前
力扣-贪心-376 摆动序列
算法·leetcode
Orange--Lin3 小时前
【用deepseek和chatgpt做算法竞赛】——还得DeepSeek来 -Minimum Cost Trees_5
人工智能·算法·chatgpt
01_3 小时前
力扣hot100 ——搜索二维矩阵 || m+n复杂度优化解法
算法·leetcode·矩阵