Thread: Where is this program going wrong?

  1. #1
    Kirby1024
    Guest

    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!

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    my suggestion is writing your own hex conversion routines. its not that hard, and it will run quicker than calling a command line program.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Kirby1024
    Guest
    I already have a Hex converter, it's not like this is urgent. I'm trying to get the hang of passing command line arguments. As I said, the program works perfectly well... As long as you only include <inputfile> and <outputfile> into the command line arguments. Placing "-d" in screws the entire program up, and I'm curious as to why, seeing how I thought I had handled it already.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    at a glance your method of conversion seems a bit dodgy. am i right that you are trying to convert using automatic typecasting?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Kirby1024
    Guest
    Yes, that's what I'm doing. It seemed the easiest solution when I wrote the original program, it did work for that program. It's probably not the best way of doing it, but it is about the simplest way of doing it, and it seems to work perfectly well.

    As I said, it's not the conversion that the program seems to be spitting at (the program does that perfectly). The actual command line switch seems to be stopping the program at some point.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    all im getting problems with is the hex conversion. my adaptation is reading numbers from one file, and spitting garbage into the other.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    19
    Hmm, how odd. Try reading this into the hexifier, I've checked it with the original version, so the hex codes I know are correct here:-

    49 20 74 72 69 65 64 20 74 6F 20 77 61 72 6E 20 79 6F 75 2C 0A 42 65 77 61 72 65 20 6F 66 20 68 65 78 65 64 20 70 6F 65 74 72 79 2C 0A 4E 6F 6F 6E 65 20 63 61 6E 20 72 65 61 64 20 69 74 2E

    You should get:-

    I tried to warn you,
    Beware of hexed poetry,
    Noone can read it.

    (Or vice versa, depending on which way you're converting)

    But that is quite strange, seeing how I've successfully implemented this kind of conversion system previously. I've just tried hexing something else with it, and it at least converts the file correctly. When I try to dehex back, Win2000 states that "hex.exe has generated errors and will be closed by windows. You will need to restart the program. An error log is being generated", although it does generate the output file.

    Edit: OK, I've been doing some extra testing, and it does look like something freaky is happening in the Hex -> Char transition. I can get past the strcmp(), after that things get weird. I'm honestly lost, since I can't see a difference between the code here that's screwing up, and the code that I'm using in the original version of the program!
    Last edited by kirby1024; 09-01-2002 at 02:44 AM.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    19
    Hmm. Well, I think I may have found the problem. For some reason, using an simple int for hex instead of an unsigned short int fixes the problem right up. Can't for the life of me figure out why though...

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    19
    Ah, cool. Thanks for that explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM