|
发表于 2022-6-11 14:51:16
|
显示全部楼层
#include <stdio.h>
#include <string.h>
/*
* feof():当设置了与流关联的文件结束标识符时,该函数返回一个非零值,否则返回零。
* C 库函数 int getc(FILE *stream) 从指定的流 stream 获取下一个字符(一个无符号字符),并把位置标识符往前移动。
* C 库函数 int fprintf(FILE *stream, const char *format, ...) 发送格式化输出到流 stream 中。
*/
int main( void )
{
unsigned char tmp;
unsigned long count = 0;
FILE *infp;
FILE *outfp;
infp = fopen( "T5L51.bin", "rb" );
outfp = fopen( "file.txt", "wb" );
fprintf( outfp, "%s", "const char arr[]={" );
/* 要先读取再判断,不然会多出一个字节 */
tmp = getc( infp );
while ( !feof( infp ) )
{
fprintf( outfp, "0x%02X,", tmp );
count++;
tmp = getc( infp );
}
fprintf( outfp, "%s", "};" );
fprintf( outfp, "\n\n//一共有%u个字节\n", count );
printf( "一共有%u个字节\n", count );
fclose( infp );
fclose( outfp );
return(0);
} |
|