OK. I started tried writing some code and this is what ended with:
If you don't want to to see all the code (it's a bit lengthy) you can just look at the main() function, that's where I think the problem is...
Code:
// Basically a program that reads a binary file (you can just drag the file onto the program) and
// outputs its contents to a file (named by the user) in HEX in an array definition
// e.g.
// short file[filesize] = {0x21,0xb3, 0x5f, ...};
#include <stdio.h>
#define VALUES_PER_LINE 20
void clrInBuff(void);
void rmExt(char fileName[]);
int rmNwLn(char string[]);
int fndExtLcn(const char fileName[]);
long int sizeofFile(FILE *file);
int main(int argc, char *argv[])
{
FILE *inFile; // binary file to be read from
FILE *outFile; // output file with array definition
char outFilename[FILENAME_MAX+1];
char inFilename[FILENAME_MAX+1];
long int inFileSize;
int valuesWritten; // variable to count number of values written
long int i;
long int freadResult;
if (argc != 2)
{
printf("Enter input filename: ");
fgets(inFilename, FILENAME_MAX, stdin);
rmNwLn(inFilename);
}
else strcpy(inFilename, argv[1]);
printf("Enter output filename: ");
fgets(outFilename, FILENAME_MAX, stdin);
rmNwLn(outFilename);
printf("opening files\n");
if ((inFile = fopen(inFilename, "rb")) == NULL)
{
printf("Error opening \"%s\"\n", inFilename);
clrInBuff();
return 1;
}
else printf("\"%s\" opened successfully\n", inFilename);
if ((outFile = fopen(outFilename, "wt")) == NULL)
{
printf("Error creating \"%s\"\n", outFilename);
clrInBuff();
return 1;
}
else printf("\"%s\" created successfully\n", outFilename);
inFileSize = sizeofFile(inFile) / sizeof(char);
rmExt(outFilename);
printf("Started outputting to files\n");
fprintf(outFile, "char %s[%ld] = \n{", outFilename, inFileSize);
valuesWritten=0;
//do
{
char *buf;
buf = (char *) malloc( sizeof(char) * inFileSize);
freadResult = fread(buf, sizeof(char), inFileSize, inFile);
for (i=0; i<freadResult; i++)
{
if (valuesWritten == VALUES_PER_LINE) fputc('\n', outFile);
if (fprintf(outFile, "%#x, ", buf[i]) < 0)
printf("Error in fprintf\n");
//printf("%#hx, ", buf[i]);
++valuesWritten;
}
free(buf);
}// while (freadResult == inFileSize);
fputs("\b\b}", outFile);
printf("Closing files\n");
fclose(inFile);
fclose(outFile);
clrInBuff();
return 0;
}
/* Clears input buffer */
void clrInBuff(void)
{
char c;
/* keep reading characters until '\n' is found (nothing after that) or EOF is reached */
while ( ((c=getchar()) != '\n') && (c != EOF) ) ;
}
/* Searches for and removes '\n' from a string */
int rmNwLn(char string[])
{
int i;
for (i=0; string[i] != '\n' && string[i] != '\0'; i++)
;
if (string[i] == '\n')
{
string[i] = '\0';
return i;
}
else
return EOF;
}
/* returns size of file */
long int sizeofFile(FILE *file)
{
long int i;
fseek(file, 0, SEEK_END);
i = ftell(file);
fseek(file, 0, SEEK_SET);
return i;
}
/* remove extension of file */
void rmExt(char fileName[])
{
int i;
i = fndExtLcn(fileName);
fileName[i] = '\0';
}
/* return location of extension in file name (after '.').
returns '\0' if no extension.
*/
int fndExtLcn(const char fileName[])
{
int i;
/* Keep searching backwards until '.' is found (extension started) */
for (i = strlen(fileName); fileName[i] != '.' ; i--)
if (!i)
{
return '\0';
}
i++;
return i;
}
For some reason, the output file is not even created... I can't really think of anything to fix this :'(
EDIT: modified the code to allow manual filename input. When I use arguments (by dragging the file onto the executable) fopen succeeds but the file is not created. However when I input the file name manually, it works fine...
I really don't understand what's going on :S