进程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;
}
相关推荐
代码雕刻家19 分钟前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
雨中rain19 分钟前
算法 | 位运算(哈希思想)
算法
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
我是哈哈hh4 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
Tisfy4 小时前
LeetCode 2187.完成旅途的最少时间:二分查找
算法·leetcode·二分查找·题解·二分
Mephisto.java4 小时前
【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
sql·算法·leetcode
robin_suli4 小时前
滑动窗口->dd爱框框
算法
丶Darling.4 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5205 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法