1.使用fwrite、fread将一张随意的bmp图片,修改成德国的国旗
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./gaoda.bmp","r");
fseek(fp,2,SEEK_SET);
int bmp_size = 0;
fread(&bmp_size,4,1,fp);
printf("文件大小为 %d 字节\n",bmp_size);
int w = 0,h = 0;
fseek(fp,18,SEEK_SET);
fread(&w,4,1,fp);
fread(&h,4,1,fp);
printf("图像尺寸为:%d * %d\n",w,h);
fclose(fp);
fp = fopen("./gaoda.bmp","r+");
fseek(fp,54,SEEK_SET);
// bmp 图片默认像素格式是 bgr的
unsigned char yel[3] = {0,255,255};
unsigned char red[3] = {0,0,255};
unsigned char bla[3] = {0,0,0};
for(int i=0;i<w;i++)
{
for(int j=0;j<(h/3);j++)
{
fwrite(yel,3,1,fp);
}
}
for(int i=0;i<w;i++)
{
for(int j=(h/3);j<(h/3*2);j++)
{
fwrite(red,3,1,fp);
}
}
for(int i=0;i<w;i++)
{
for(int j=(h/3*2);j<h;j++)
{
fwrite(bla,3,1,fp);
}
}
fclose(fp);
return 0;
}
2.使用提供的getch函数,编写一个专门用来输入密码的函数,要求输入密码的时候,显示 * 号,输入回车的时候,密码输入结束
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <assert.h>
int getch()
{
int c = 0;
struct termios org_opts, new_opts;
int res = 0;
res = tcgetattr(STDIN_FILENO, &org_opts);
assert(res == 0);
new_opts = org_opts;
new_opts.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c = getchar();
res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res == 0);
return c;
}
void password()
{
char arr[6]={'1','2','3','4','5','6'};
char pass[32]={0};
int i=0;
printf("请输入密码:");
while(1)
{
char c=getch();
if(c=='\n'){break;}
printf("*");
pass[i++]=c;
}
int a=1;
for(i=0;i<6;i++)
{
int k=strlen(pass);
if(k!=6)
{
a=0;
break;
}
else if(pass[i]!=arr[i])
{
a=0;
break;
}
}
if(a)
{
printf("\n密码正确\n");
}
else
{
printf("\n密码错误,请重新输入:\n");
password();
}
fflush(stdin);
}
int main(int argc, char *argv[])
{
password();
return 0;
}