Created
January 18, 2015 01:36
-
-
Save baylej/49ebf2e578d57430f6c7 to your computer and use it in GitHub Desktop.
mkmemfile.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
const int BYTES_PER_LINES = 20; | |
int main(int argc, char *argv[]) { | |
FILE *in = NULL, *out = NULL; | |
int i, cpt=0, first=1; | |
size_t val; | |
unsigned char value[BYTES_PER_LINES]; | |
if (argc < 2 || argc > 3) { | |
printf("%s, converts a file to a char array you can use in your programs.\n", argv[0]); | |
printf("Usage : %s path_to_source_file [name_of_the_output_file]\n", argv[0]); | |
return -1; | |
} | |
if (!(in = fopen(argv[1], "rb"))) { | |
fputs("Failed to open source file !\n", stderr); | |
return -1; | |
} | |
if (argc != 3) | |
out = stdout; | |
else if(!(out = fopen(argv[2], "wb"))) { | |
fputs("Failed to open output file !\n", stderr); | |
return -1; | |
} | |
fputs("static const char file_array[] = {\n", out); | |
while ((val = fread(value, sizeof(char), BYTES_PER_LINES, in)) > 0) { | |
if (!first) { | |
fputs(",\n", out); | |
} | |
else first = 0; | |
fputc('\t', out); | |
for (i = 0; i < val; i++) { | |
fprintf(out, "0x%2.2x", value[i]); | |
if (i != val - 1) | |
fputs(", ", out); | |
} | |
cpt += val; | |
} | |
fputs("};\n", out); | |
fprintf(out, "static const int len = %d;\n", cpt); | |
fclose(in); | |
fclose(out); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment