用发write、fread将一张随意图片修改成德国国旗
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
FILE* fp=fopen("./122.bmp","r");
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("./122.bmp","r+");
fseek(fp,54,SEEK_SET);
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*2/3);j++){
fwrite(red,3,1,fp);
}
}
for(int i=0;i<w;i++){
for(int j=(h*2/3);j<h;j++){
fwrite(bla,3,1,fp);
}
}
fclose(fp);
return 0;
}
使用getch函数编写一个专门用来输入密码的函数,要求输入密码时显示*号输入回车,密码输入结束
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<termios.h>
#include<sys/types.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;
}
void sec(char *sec,int len)
{
int i=0;
while(i<len-1){
char c=getch();
if(c==10){break;}
printf("*");
sec[i++]=c;
}
sec[i]='\0';
printf("\n");
}
int main(int argc, char *argv[])
{
char arr[10];
sec(arr,10);
printf("输入密码为:%s\n",sec);
return 0;
}