Hello Everyone,
I successfully used the following code to create a text file (for use in an assembly file) from a binary file.
The problem is the code adds 2 extra bytes at the end (, 0xFFFF,). I think it has something to do with the EOF string, but I can't figure it out. I have tried for a few days now and I just can't see the problem.
Can someone help me please?
This code works and produces a text file from a binary file that looks like this:
Code:
.db 0x49, 0x44, 0x33, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x76, 0x50, 0x52, 0x49, 0x56, 0x00, 0x00
Here is the code:
Code:
#include <stdio.h>
/*program works good except we get an extra word at the end
like this , 0xFFFF, (shouldn't be there at all)*/
FILE *fin, *fout;
int main(int argc, char **argv)
{
unsigned short val, word_count;
// char buffer[10];
char buffer[20];
printf ("argv[1]=%s\n", argv[1]);
fin = fopen(argv[1], "rb");
fout = fopen(argv[2], "wt");
word_count = 0;
while (!feof(fin)) {
if (word_count == 0) {
fwrite("\t.db\t",1,5,fout);
}
val = fgetc(fin);
if (word_count < 15) {
sprintf(buffer, "0x%02X, ", val);
word_count++;
}
else {
sprintf(buffer, "0x%02X\n", val);
word_count = 0;
}
fwrite(buffer, 1, strlen(buffer), fout);
}
fclose(fin);
fclose(fout);
return (0);
}