I am writing a program that reads a fiel and displays the contents in hex values.
The problem is that there are some very strange characters in my output.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BUFFER_LEN 512
typedef enum cbool { ctrue = 1, cfalse = 0 } cbool;
struct operation_info
{
char *infilename;
char *outfilename;
cbool out;
} op_info;
int dump_file()
{
FILE *infile;
FILE *outfile;
char buffer[MAX_BUFFER_LEN];
int times;
int fsize;
int counter;
int fcount;
// Create the input file.
infile = fopen(op_info.infilename, "rb");
if (infile == NULL)
{
perror("Could not open file for reading!");
return -1;
}
// Create the ouput file if needed.
if (op_info.out == ctrue)
{
outfile = fopen(op_info.outfilename, "wb");
if (outfile == NULL)
{
perror("Could not open file for writing!");
return -1;
}
}
// Get the size of the file.
fseek(infile, 0, SEEK_END);
fsize = ftell(infile);
fseek(infile, 0, SEEK_SET);
// Get how many times need to be read from the file.
if ((fsize < MAX_BUFFER_LEN) || (fsize == MAX_BUFFER_LEN))
times = 1;
else
times = fsize / MAX_BUFFER_LEN + 1;
// Read from the file.
counter = 0;
while (counter < times)
{
fread(buffer, 1, MAX_BUFFER_LEN, infile);
counter++;
if (op_info.out == ctrue)
{
for (int i = 0; i < strlen(buffer); i++)
fprintf(outfile, "%c", buffer[i]);
fflush(outfile); // Make sure everything is written to the file.
}
fcount = 0;
for (int i = 0; i < strlen(buffer); i++)
{
printf("%.2x ", buffer[i]);
if (fcount == 16)
{
printf(": ");
for (int k = i - 16; k < i; k++)
{
if (((int)buffer[k] >= 32) && ((int)buffer[k] <= 126))
printf("%c", buffer[k]);
else
printf(".");
}
printf("\n");
fcount = 0;
}
fcount++;
}
}
// Close the file.
fclose(infile);
if (outfile)
fclose(outfile);
return 0;
}
int main(int argc, char **argv)
{
op_info.infilename = "C:\\Users\\User\\Desktop\\hexdump.c";
op_info.out = ctrue;
op_info.outfilename = "C:\\Users\\User\\Desktop\\hexdump.txt";
dump_file();
getchar();
return 0;
}
Sample output of strange characters (not all the output):
// Create the input file.
infile = fopen(op_info.infilename, "rb");
if (infile == NULL)
{
perror("Could not open file ÌÌÌÌÌÌÌÌpsLÌÌÌÌÌÌÌÌPsLÌÌÌ̬ãÁœú©for reading!");
return -1;
}
How do I get rid of all those ÌÌÌÌÌÌÌÌpsL
characters?
Also, when ever I am printing the contents of buffer, some extra data that are left behind from the last time something was read into it also prints. How do I 'clean' the buffer so that only the current contents are in it?
Sample output:
int main(int argc, char **argv)
{
op_info.infilename = "C:\\Users\\User\\Desktop\\hexdump.c";
op_info.out = ctrue;
op_info.outfilename = "C:\\Users\\User\\DesktÌÌÌÌÌÌÌÌpsLÌÌÌÌÌÌÌÌPsLÌÌÌ̬ãÁœú©op\\hexdump.txt";
dump_file();
getchar();
return 0;
} <!-- This is suppose to be the end, but then the code from below shows up. -->
)buffer[k] >= 32) && ((int)buffer[k] <= 126))
printf("%c", buffer[k]);
else
printf(".");
}
printf("\n");
fcount = 0;
}
Also, how can I check if a file has been opened or not. Because:
if (outfile)
fclose(outfile);
causes an error if I set op_info.out = ctrue;
to op_info.out = cfalse;
Thanks in advance.