使用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("./2.bmp","r");fseek(fp,2,SEEK_SET);int bmp=0;fread(&bmp,4,1,fp);printf("%d\n",bmp);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("./2.bmp","r+");fseek(fp,54,SEEK_SET);unsigned char bgr1[3] = {0,0,0};unsigned char bgr2[3] = {0,0,255};unsigned char bgr3[3] = {0,255,255};char *p=bgr3;for(int i=0;i<w;i++){for(int j=0;j<h/3;j++){fwrite(p,3,1,fp);}}for(int i=0;i<w;i++){for(int j=h/3;j<2*h/3;j++){p=bgr2;fwrite(p,3,1,fp);}}for(int i=0;i<w;i++){for(int j=2*h/3;j<h;j++){p=bgr1;fwrite(p,3,1,fp);}}fclose(fp);return 0;
}
使用提供的getch函数,编写一个专门用来输入密码的函数,要求输入密码的时候,显示 * 号,输入回车的时候,密码输入结束
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int getch();
void inpassw(char *passw, int len)
{int i = 0;char c;while (i < len-1){c = getch();if (c == '\n') {break;}if (c == 127) {if (i > 0) {printf("\b \b");i--;}}else {passw[i] = c;printf("*");i++;}}passw[i] = '\0';printf("\n");
}int main(int argc, const char *argv[])
{char passw[20];printf("输入密码: ");inpassw(passw, 20);printf("输入的密码为: %s\n", passw);return 0;
}
//getch.c
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.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 | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);c=getchar();res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);assert(res==0);return c;
}
思维导图