io

第一题

include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;    int main(int argc, const char *argv[])

typedef struct {
    unsigned char blue;
    unsigned char green;
    unsigned char red;
} RGBPixel;

int main() {
    FILE *file = fopen("1.bmp", "rb");
    if (file == NULL) {
        printf("无法打开文件
");
        return 1;
    }


    fseek(file, 14, SEEK_SET);
    unsigned int width, height;
    fread(&width, 4, 1, file);
    fread(&height, 4, 1, file);


    int colorTableSize = (width * height) % 4 == 0 ? 0 : 4 - (width * height) % 4;
    fseek(file, colorTableSize, SEEK_CUR);


    RGBPixel firstPixel, lastPixel;
    fread(&firstPixel, sizeof(RGBPixel), 1, file);
    fseek(file, -sizeof(RGBPixel), SEEK_END);
    fread(&lastPixel, sizeof(RGBPixel), 1, file);

    printf("第一个像素点的BGR值为:(%u, %u, %u)
", firstPixel.blue, firstPixel.green, firstPixel.red);
    printf("最后一个像素点的BGR值为:(%u, %u, %u)
", lastPixel.blue, lastPixel.green, lastPixel.red);

    fclose(file);
    return 0;
}
第二题

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;    int main(int argc, const char *argv[])
{
    FILE* rfp = fopen("1.bmp","r+");
    if(rfp==NULL)
    {
        perror("rfp fopen");
        return -1;
    }
    int size = 0;
    fseek(rfp,2,SEEK_SET);
    fread(&size,4,1,rfp);
    printf("size = %d\n",size);
    int res_heigth = 0,res_width = 0;
 
    fseek(rfp,18,SEEK_SET);
    fread(&res_width,4,1,rfp);
 
    fseek(rfp,22,SEEK_SET);
    fread(&res_heigth,4,1,rfp);
 
    printf("res_width = %d   res_heigth = %d\n",res_width,res_heigth);
 
    
    int width = res_width*2;
    int heigth = res_heigth*2;
 
    size = width*heigth+54;
    fseek(rfp,2,SEEK_SET);
    fwrite(&size,4,1,rfp);
 
    fseek(rfp,22,SEEK_SET);
    fwrite(&heigth,4,1,rfp);
    
    fseek(rfp,18,SEEK_SET);
    fwrite(&width,4,1,rfp);    
 
    unsigned black[3] = {0,0,0};
    fseek(rfp,54,SEEK_SET);
    for(int i = 0;i<width;i++)
    {
        for(int j = 0;j<heigth;j++)
        {
        fwrite(black,3,1,rfp);
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值