这是我在C
中提取的代码 #include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, char *argv[])
{
if(argc != 3)
{
fprintf(stderr, "Usage >NameProgram firstParam Executable.exe\n");
return -1;
}
FILE *output = fopen("output.txt", "a");
[..]
char* input_file = argv[2]; //the name of the exe
FILE* f_input = fopen(input_file, "rb");
fprintf(output,"char byn[] = {\n");
unsigned long n = 0;
while(!feof(f_input))
{
unsigned char c;
if(fread(&c, 1, 1, f) == 0)
break;
fprintf(output,"0x%.2X,", (int)c);
++n;
if(n % 10 == 0)
fprintf(output,"\n");
}
fclose(f_input);
fclose(output);
//truncating file
FILE *output = fopen("output.txt", "r+");
fseek(output, -1, SEEK_END);
fprintf(output,"};\n\n");
fclose(output);
[..]
答案 0 :(得分:0)
使用您发布的代码作为指南,我制作了以下内容:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc != 3)
{
fprintf( stderr, "USAGE: %s outFilename inFileName\n", argv[0] );
exit( EXIT_FAILURE );
}
FILE *fp_out = fopen( argv[1], "a");
FILE* fp_in = fopen( argv[2], "rb");
fprintf(fp_out, "char byn[] = {\n" );
unsigned long n = 0;
unsigned char c;
while(fread(&c, 1, 1, fp_in) == 1)
{
fprintf( fp_out,"0x%.2X,", (int)c);
++n;
if(n % 10 == 0)
fprintf( fp_out,"\n");
}
fprintf( fp_out, "};\n\n");
fclose(fp_in);
fclose(fp_out);
} // end function: main
我在一些可执行文件上运行
注意:我正在运行Ubuntu Linux 14.04
它似乎工作正常。
与您的示例一样,我跳过了错误检查(您应该将其真正包含在实际代码中。)