将图读成数组保存(也可以是普通文件)
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为数组内容。没有去做从文件中读。。。有兴趣的可以去玩玩。
#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为数组内容。没有去做从文件中读。。。有兴趣的可以去玩玩。
相关文章推荐
- 数组和字典的writeToFile方法——在项目开发中处理网络数据的时候,可以把请求获得的网络数据保存为plist文件,这样更方便开发
- 文件上传并可以批量编辑修改保存
- 怎么让普通用户达到root用户也可以拥有权限修改文件(CentOS系统)
- Qt简单编程--数组保存到文件
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 数组保存为txt, npy, csv 文件, 数组遍历enumerate的方法
- 字节数组用txt文件保存工具类
- 作业二:将字符串数组把重复数据剔除后写入一个文件保存起来。
- 以普通用户启动的Vim如何保存需要root权限的文件
- 数组 保存为 txt, npy, csv 文件, 数组遍历enumerate
- php 将数组结果作为返回值保存到php文件中
- 『Bash Script』忽略权限问题-强制修改保存普通用户只读文件_knityster -=编织者=-_百度空间
- 可以用save name x y 保存工作空间变量 x y到文件 name.mat
- C0302 将一个代码块中的内容保存在文件中, 查看一个rpm包是否可以安装
- passwd命令读取的shadow文件没有任何权限,为什么普通用户可以正常登录
- 把filedia设置为0可以不弹出对话框保存文件
- Android资源文件中保存字符串数组详解
- python 数组保存到json 文件
- 『Bash Script』忽略权限问题-强制修改保存普通用户只读文件_knityster -=编织者=-_百度空间
- 如何使用js将canvas保存为图片文件,并且可以自定义文件名