Where is this program going wrong?
I'm trying to make a command-line Hexifier (ie it turns all the characters in a file into their Hexadecimal Value equivalents.
Now, It works when I'm converting to hex, but converting back causes some sort of error (I'm using Win2000, and it effectively causes the "This program has caused an error and must be shut down" message).
Here is the full length of the program:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) //Command Argument Format: hex <inputfile> <outputfile> {-d}
{
//Declaration of Variables
FILE * in = fopen(argv[1], "r");
FILE * out = fopen(argv[2], "w");
unsigned short int hex;
char letter;
//Manual Output
if(argc == 1)
{
printf("Format of Command Line Hex: hex <inputfile> <outputfile> {-d}\n\n");
printf("<inputfile> File to be read from\n");
printf("<outputfile> File to read De/Hexed output to\n");
printf("{-d} De-hex code\n");
exit(4);
}
//Input File Check
if(in == NULL)
{
fprintf(stderr, "Error - Input file %s not found", argv[1]);
exit(1);
}
//Output File Check
if(out == NULL)
{
fprintf(stderr, "Error - Output file %s not available", argv[2]);
exit(2);
}
if(argc == 4)
{
//De-hexification code
if(strcmp(argv[3], "-d") == 0)
{
while(1)
{
if(fscanf(in, "%X", &hex)!= EOF)
{
letter = hex;
fprintf(out, "%c", letter);
}
else
{
fclose(in);
fclose(out);
return 0;
}
}
return 0;
}
else
{
fprintf(stderr, "Error - Unrecognised switch");
exit(5);
}
}
//Hexification code
else
{
while(1)
{
if(fscanf(in, "%c", &letter)!= EOF)
{
hex = letter;
fprintf(out, "%2X ", hex);
}
else
{
fclose(in);
fclose(out);
return 0;
}
}
}
}
I have the oddest feeling it's due to something in strcmp(), but I can't be sure. Basically, when I try and put in the "-d" at the command prompt to dehex something, it breaks down. It compiles perfectly well, with the only error being that there's no return statement at the end of the code (as you can see, I've taken care of that elsewhere)
Someone help!