您的位置:首页 > 其它

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

2008-09-26 16:16 218 查看
以前写的代码,后面发现还挺实用的:),贴出来保存。
#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为数组内容。没有去做从文件中读。。。有兴趣的可以去玩玩。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐