Thread: Something screwy going on here...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    19

    Something screwy going on here...

    OK, Here is a program that was working perfectly fine a few days ago, but has suddenly decided to begin depositing 56MB files on my computer (at least, that's how far it gets before I shut it down). It doesn't give any errors on lcc-win, and I wouldn't have though it was so complex that the error would just pop up.

    So you know, it's a command-line hex <=> ASCII converter. It works through typecasting, because I figured I'd be slack and not bother with the union.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]) //Command Argument Format: hex <inputfile> <outputfile> {-d}
    {
    	//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);
    	}
    
    	//Declaration of Variables
    	FILE * in = fopen(argv[1], "r");
    	FILE * out = fopen(argv[2], "w");
    	unsigned short int hex;
    	char letter;
    
    	//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("-d", argv[3]) == 0)
    		{
    			while(fscanf(in, "%hX ", &hex) != EOF)
    			{
    				letter = hex;
    				fprintf(out, "%c", letter);
    			}
    			fclose(in);
    			fclose(out);
    			return 0;
    		}
    
    		else
    		{
    			fprintf(stderr, "Error - Unrecognised switch");
    			fclose(in);
    			fclose(out);
    			exit(5);
    		}
    	}
    
    	//Hexification code
    	else
    	{
    		while(fscanf(in, "%c", &letter) != EOF)
    		{
    			hex = letter;
    			fprintf(out, "%2hX ", hex);
    		}
    		fclose(in);
    		fclose(out);
    		return 0;
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    19
    Really? Bugger. It's with the -d switch.

    I'll try changing the structure then, to get the decs done first.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    19
    Hmm. Binary files shouldn't be coming near it. It's supposed to take a Text file full of text, turn it into a Text file full of Hexadecimal numbers, and with -d, do the reverse. Sorry if that wasn't clear before.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    19
    OK, I managed to solve the problem. I fixed a few bits and pieces, and finally caved in and used a union.

    So, it looks like the program is working OK now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. screwy output
    By ssjnamek in forum C Programming
    Replies: 15
    Last Post: 01-27-2006, 01:50 AM
  3. Screwy school fight rules
    By windoze victim in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 05-18-2003, 09:27 AM
  4. file existance getting screwy on me...
    By Shadow in forum C Programming
    Replies: 15
    Last Post: 02-16-2002, 01:43 PM