将图读成数组保存(也可以是普通文件)

以前写的代码,后面发现还挺实用的:),贴出来保存。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc ,char *argv[])
{
    FILE *fpin    = NULL;
    FILE *fpout = NULL;
   
    if(argc != 3)//参数顺序:可执行文件 图文件名 待写入文件名
    {   
        printf("Error!!!/n");
        return -1;
    }
   
    fpin = fopen(argv[1],"r");
    if(NULL == fpin)
    {
        return -1;
    }
    fpout = fopen(argv[2],"w+");
    if(NULL == fpout)
    {
        fclose(fpin);
       
        return -1;
    }
    int c = 0;
    int num = 0;
    unsigned char str[20];
   
    unsigned char c_str1[50];
    unsigned char c_str2[] = "/n};";
   
    memset(c_str1,0,sizeof(c_str1));
    sprintf(c_str1,"static const unsigned char %s[] = {/n",argv[0] + 2);
   
    fputs(c_str1,fpout);
   
    memset(str,0,sizeof(str));   
    while((c = fgetc(fpin)) != EOF)
    {
        num++;       
        sprintf(str," 0x%02x,",c);
        fputs(str,fpout);
        if(num % 8 == 0)
        {
            sprintf(str,"/n");
            fputs(str,fpout);
        }
    }
   
    if(num == 0)
        fseek(fpout,-2,SEEK_END);
    else
        fseek(fpout,-1,SEEK_CUR);
       
    fwrite(c_str2,sizeof(c_str2),1,fpout);
    printf("OK!/n");
   
    fclose(fpin);
    fclose(fpout);
   
    return 1;
}

随便把数组转成图的也收藏:)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const unsigned char cont_bmp[] = {
   
};

#define PATH "keyboard.bmp"

int main(int argc ,char *argv[])
{
    FILE *fp = NULL;
    int c;
    int n,i = 0;
   
    fp = fopen(PATH,"w+");
    if(fp == NULL)
    {
        printf("fopen error!/n");
        return 0;
    }
   
    n = sizeof(cont_bmp);
    for(i = 0;i < n;  i++)
    {
        //sprintf(c,"%d",cont_bmp[i]);
        fputc(cont_bmp[i],fp);
    }
   
    fclose(fp);
   
    return 0;
}
//cont_bmp为数组内容。没有去做从文件中读。。。有兴趣的可以去玩玩。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值