dump array into file in "hex format"
Hi, on my job we need to dump the content of an array ( for debug purpose ). As i'm not a very experienced programmer, i tried to code something generic ( Yes i read it here: write generic code :) )
This is what i have so far:
Code:
/*
* Dump content of array into file, by A.lam
*/
#include <stdlib.h>
#include <stdio.h>
int PrintArray(char *filename, void *array, int arraySize, int elementSize);
int main()
{
int array[9]= { 0x10, 0xff, 0xaa, 0x11, 0x33, 0x55, 0x77, 0x22, 0x33 };
int i = 0;
PrintArray("c:\\log3.txt", array, sizeof(array), sizeof(int));
system("PAUSE");
return 0;
}
int PrintArray(char *filename, void *array, int arraySize, int elementSize)
{
FILE *fp;
int i=0;
if(!(fp = fopen(filename, "w")))
//oops, we could not open a file for writing, exiting
return(-1);
//printout the entire array in hex notation
fprintf(fp,"\n--------- begin printout array --------------\n");
for(i=0; i < arraySize/elementSize; i++ ) {
fprintf(fp, "0X%X ", ((int*)array)[i]);
/// WHAT IF I WANT TO PRINTOUT ANOTHER TYPE ???? ///
//after 8 bytes we want to have a newline
if(!((i+1)%8))
fprintf(fp, "\n");
}
fprintf(fp,"\n---------- end printout array --------------");
fclose(fp);
//everything went well
return(0);
}
It works ok but in this example it can only printout int arrays, but what if i want to printout a char or double array.
I want to write nice clean code :). Can someone give me a hint, and tell me what you think of my code..