When you have it as ASCII characters 4 at a time, what are you going to do next with the data?
Do you want the whitespace between them?
Do you want the address printed every 8 16-words? If so, how do you want it separated from the file data?
It appears that your processor is little endian. So do you want the bytes in hex or the 16-bit words in hex or in 32-bit words in hex?
Do you understand how to manipulate your program in message 1 and 3 to print the data in hexadecimal instead of a string? (Hint: Use the %0x or %0X format in the printf (or fprintf or sprintf) statement.)
I am reluctant at this point to post my draft code because:
1) It is untested.
2) Except for the code you gave in messages 1 and 3, you have not posted any revisions. There is a homework policy in this forum. My code would take you from almost no code to a nearly complete solution (unless of course I have a nasty subtle bug in it that I have not spotted). If you want more help, please show that you can use sprintf( ) into a buffer in hexadecimal format. Or ask specific questions about sprintf( ) and the hexadecimal format conversions.
OK. Here is some partially tested code that does what you probably want but is does NOT do it like the "od -x" command. The following is NOT the code I drafted earlier. It is much closer to the code in messages 1 and 3. Since it is NOT extensively tested, use at your own risk and review comments would be appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#define FILE_BLOCK 256u
#define HEX_CHAR_PER_BYTE 2u
int
main (int argc, char **argv)
{
FILE * in;
size_t wordidx;
size_t words_read;
unsigned short filebuff[FILE_BLOCK];
char convbuff[FILE_BLOCK*sizeof(filebuff[0])*HEX_CHAR_PER_BYTE+1];
in = fopen(argv[1], "rb");
if(in == NULL) {
fprintf (stderr, "Can't open file.\n");
return EXIT_FAILURE;
}
while ((words_read = fread(filebuff, sizeof(filebuff[0]), FILE_BLOCK, in)) > 0 ) {
for ( wordidx = 0; wordidx < words_read ; wordidx++)
sprintf(
&convbuff[wordidx*sizeof(filebuff[0])*HEX_CHAR_PER_BYTE],
"%04X",
filebuff[wordidx]
);
/* process each convbuff here. */
}
fclose(in);
return 0;
}
Warnings:
This was written assuming CHAR_BITS is 8 and sizeof(unsigned short) is 2.
If an implementation uses a stack for its automatic variables, this implementation will require more than 512+1024+4+4 and one FILE * bytes on the stack. Make sure you configure your environment for enough stack.
The place I am most likely to have errored is in the %04X format conversion specification. Of course, there are plenty of other places where I could have errored.