3.1 IO进程线程

使用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;
}

 思维导图

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值