第一题
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;
}