Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1137417
  • 博文数量: 141
  • 博客积分: 2853
  • 博客等级: 少校
  • 技术积分: 2266
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-04 12:03
文章分类

全部博文(141)

文章存档

2014年(3)

2013年(12)

2012年(126)

分类: 嵌入式

2012-11-20 13:24:55

#include
#include
#include

#define LINE_MAX 16
int ftoc ( char * p_read_file, char * p_write_file )
{
char buffer[64] = {0};
FILE *h_read     = NULL;
FILE *h_write    = NULL;
int read_count  = 0;
int write_count = 0;
int line_max = 0;
unsigned char byte;
h_read = fopen( p_read_file, "rb" );
if ( h_read == NULL )
{
return 0;
}
h_write = fopen( p_write_file, "w" );
if ( h_write == NULL )
{
fclose( h_read );
return 0;
}


fwrite( "const unsigned char html[] = {\n", 1, strlen("const unsigned char html[] = {\n"), h_write );

do
{
byte = 0;
memset( buffer, 0, 64 );
read_count = fread(&byte, 1, 1, h_read);
if ( read_count == 1 )
{
sprintf(buffer , "0x%02x,", byte );
write_count = fwrite( buffer, 1, strlen(buffer), h_write );
if ( line_max >= (LINE_MAX-1) )
{
line_max = 0;
write_count = fwrite( "\n", 1, strlen("\n"), h_write );
}
else
{
line_max++;
}
}
}while ( read_count == 1 );

fwrite( "\n};", 1, strlen("\n};"), h_write );
fclose(h_read);
fclose(h_write);
return 1;
}

int main(int argc, char* argv[])
{
if ( argc < 3 )
{
printf("usage: ftoc file1 file2 ");
return 0;
}

ftoc( argv[1], argv[2] );

return 0;
}
阅读(3253) | 评论(0) | 转发(0) |
0

上一篇:Http协议

下一篇:关于静态函数

给主人留下些什么吧!~~