03.01

使用fwrite、fread将一张随意的bmp图片,修改成德国的国旗

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    /*FILE* fp = fopen("./QQ图片20240301145209.bmp","r");

    fseek(fp,2,SEEK_SET);
    int bmp_size = 0;
    fread(&bmp_size,4,1,fp);
    printf("文件大小为 %d 字节\n",bmp_size);*/

    FILE* fp = fopen("./QQ图片20240301145209.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);


    fseek(fp,54,SEEK_SET);
    unsigned char pix[3] = {0, 255, 255};
    unsigned char pjx[3] = {0, 0, 255};
    unsigned char pkx[3] = {0, 0, 0};
    for(int i=0; i<w; i++)
    {
        for(int j=0; j<h/3; j++)
        {
                fwrite(pix,3,1,fp);
        }
    }
    for(int i=0; i<w; i++)
    {
        for(int j=h/3; j<(2*h/3); j++)
        {
                fwrite(pjx,3,1,fp);
        }
    }
    for(int i=0; i<w; i++)
    {
        for(int j=2*h/3; j<=h; j++)
        {
                fwrite(pkx,3,1,fp);
        }
    }
    fclose(fp);
    return 0;
}
效果

使用提供的getch函数,编写一个专门用来输入密码的函数,要求输入密码的时候,显示 * 号,输入回车的时候,密码输入结束

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int getch();

char* getpswd(char* pswd){
    int i = 0;// 用来控制pswd数组的下标
    char ch = 0;// 用来接受getch的返回值
    while(1){
        ch = getch();
        if(ch == '\n'){
            pswd[i] = 0;
            return pswd;
        }else if(ch == 127){
            printf("\b \b");
        //    fflush(stdout);
            if(i>=1){
                i--;
            }
        }else{
            printf("*");
            pswd[i++] = ch;
        }
    }
}

int main(int argc, const char *argv[])
{
    char pswd[32] = {0};
    getpswd(pswd);

    printf("pswd = %s\n",pswd);
    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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值