Thread: ASCII Art Decoder and Encoder

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    ASCII Art Decoder and Encoder

    I don't know how appropriate this question is. sorry before hand if it's not appropriate.

    I've been working on an assignment, where the program through cygwin reads a file if it is .rle it will decode the *010X ...to 10 X's like so XXXXXXXXXX ...else if it is a .txt file with the ASCII art in it, it will encode it into 3 run-length ...*010X.

    Every rle file begins with *R ...as a mark for the special characters it needs to look for. I need to use two .c files and .h file.

    As of now i have no errors. But i'm stuck on the logic. In the header file ....i have defined a few things the main ones as so. Note the functions have been declared in there too...i just didn't post all of it.

    file: rlefuncs.h
    Code:
    #define IND '*'
    #define MARK 'R'
    #define MIN_CHAR 3
    #define MAX_CHAR 5
    In the .c main file ...it should read the file and distinguish between what it should call. Either the decoder or encoder depending on what the file begins with. file: rle.c ...works perfectly fine.

    My problem is ...that when i try to decode, it does nothing. I'm not sure if this is even a good question to ask. have i miss understood the logic. can someone pm me or email me at [email protected] ...so i can show you the code. I don't wish to post it, incase the teacher thinks i copyed and pasted it off here. :S

    And if this question and the fact that i'm not posting the code here isn't what i'm supposed to do...then i'm sorry. just ignore or delete this post if i've broken any rules.

    Thanks!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can someone pm me or email me at [email protected]
    This is an open forum, post your code if you want help with it.

    >I don't wish to post it, incase the teacher thinks i copyed and pasted it off here.
    Unless you really did, your post should be the first appearance of the code and you shouldn't have much trouble proving that that is your forum account.

    >when i try to decode, it does nothing.
    That would be a problem that we can only diagnose with code. Sorry.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    okay. This is all mine... been working on it all day and night. I'm a beginner, so i have not much background.

    Code:
    int Decode(void) {
    
    	int counter = 1,currChar,i,flag=0;
    	
    	currChar = getchar();  /*Get character */
    	currChar = getchar();  /*Get character */
    	currChar = getchar();  /*Get character */
    
    	while(currChar != EOF) {   /*While currChar is not EOF wait to flag */
    		flag = 0;      
    		if(currChar == IND) {  /*If the current character is * get next character */
    			counter = getchar();  /*counts same characters */
    			if(counter < 10) {      /*If counter is less than 10 get current character */
    				currChar = getchar();
    				for(i=0; i < counter; i++) {  /*as long as i is less than counter increment */
    					putchar(currChar);
    				}
    			}
    			else {
    				if(counter == IND) { /*if counter equals '*' */
    						flag = 1;
    						putchar(currChar);  /*put '*' in variable currChar */ 
    						currChar = counter;  
    				} else {
    						putchar(currChar);
    						putchar(counter);
    				}
    			}
    		} else { putchar(currChar); }
    		if(flag==0) {        /* reset flag to normal */
    			currChar = getchar(); /*Get next character */
    		}
    	}
    	return 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Can you give me an example of the encoded file as well as what it should look like unencoded? A few lines are more than enough.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    There are a few but this is one.

    Encoded:
    Code:
    *R*014 *4X
    *013 X*4 XX
    *012 X  *3*  X*016 *5X
    *011 X  *5*  X*012 *3X*5 XX
    *8 *4X *7* *3X*6 *4X*010 XX
    *6 XX*3 X *6*  *9X*016 XX *3X
    *4 XX*6 X *4*  X*027 X*2* X
    *3 X*8 XX*4 XX*5 X*022 X*3*X
      X*9 //*4X*7 X*022 *4X
     X*9 //*3 X*029 XX
    X*9 //*4 X*010 *018X/
    X*5 *3X//*4 X*010 X
    X*4 X*3 X*5 X*9 X
    X*4 X*4 X*4 X*8 X
     X*3 X*4 X*4 X*8 X*020 XX
     X*4 X*3 X*4 X*8 X*017 *3X  XX
      X*4 *3X*6 X*8 X*015 X  X X  X
      X*013 X*9 X*014 XX X  *4X
    *3 X*013 X*9 *8X\*5 XX*3 XX  X
    *4 XX*012 XX*014 X*5 X*4 X  XX
    *6 XX*012 *4X*3 *6X/*5 X*5 *4X
    *8 *3X*013 XX*3**9 X*5 X
    *011 *013X *1**3 *1**7 X*5 X
    *024 *1**3-*1* X*5 X*5 X
    *023 *1*-*1* *1**3 *3X X*5 X
    *023 *1*- *1**7 *3X*3 X
    *022 *1*- *1*X*010 *3X
    *022 *1*- *1*X  X*010 *3X
    *021 *1*- *1*X*4 X*012 XX
    *021 *1*- *1*XX*4 X*013 X
    *020 *1*  *1*X*1* X*4 X*013 X
    *020 *1*  *1*X *1* X*4 X*013 X
    *019 *1*  *1* X*2*  X*3 *4X*010 X
    *019 *1*  *1* X*2*  XX*5 X*010 X
    *018 *1*  *2* X*2* X*5 XX*010 X
    *018 *1*  *2*  X*1*  *3X*3 X*9 X
    *017 *1*  *2**4 XX*3 *4X*7 *3X
    *016 *1*  *1* *1**6 *4X*6 X*5 X
    *015 *1**3 *1* *1**010 X*5 X*5 X
     *7=*7**3 *1* *1**011 X*5 X*6 *8X\
    *8 *1**9 *1* *1**6 /*5X*6 *8X\*6 )
    *3 *5=*010*  *1**5 X*021 )  \  )
    *5 *4=*1**9 *1**5 X*015 \  \*3 )*5X
    *9=*010**7 *022X
    
    
    *019 CURSE YOU, RED BARON!
    decoded:
    Code:
                  XXXX
                 X    XX
                X  ***  X                XXXXX
               X  *****  X            XXX     XX
            XXXX ******* XXX      XXXX          XX
          XX   X ******  XXXXXXXXX                XX XXX
        XX      X ****  X                           X** X
       X        XX    XX     X                      X***X
      X         //XXXX       X                      XXXX
     X         //   X                             XX
    X         //    X          XXXXXXXXXXXXXXXXXX/
    X     XXX//    X          X
    X    X   X     X         X
    X    X    X    X        X
     X   X    X    X        X                    XX
     X    X   X    X        X                 XXX  XX
      X    XXX      X        X               X  X X  X
      X             X         X              XX X  XXXX
       X             X         XXXXXXXX\     XX   XX  X
        XX            XX              X     X    X  XX
          XX            XXXX   XXXXXX/     X     XXXX
            XXX             XX***         X     X
               XXXXXXXXXXXXX *   *       X     X
                            *---* X     X     X
                           *-* *   XXX X     X
                           *- *       XXX   X
                          *- *X          XXX
                          *- *X  X          XXX
                         *- *X    X            XX
                         *- *XX    X             X
                        *  *X* X    X             X
                        *  *X * X    X             X
                       *  * X**  X   XXXX          X
                       *  * X**  XX     X          X
                      *  ** X** X     XX          X
                      *  **  X*  XXX   X         X
                     *  **    XX   XXXX       XXX
                    *  * *      XXXX      X     X
                   *   * *          X     X     X
     =======*******   * *           X     X      XXXXXXXX\
            *         * *      /XXXXX      XXXXXXXX\      )
       =====**********  *     X                     )  \  )
         ====*         *     X               \  \   )XXXXX
    =========**********       XXXXXXXXXXXXXXXXXXXXXX
    
    
                       CURSE YOU, RED BARON!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're trying too hard.
    Code:
    int decode ( FILE *in, FILE *out )
    {
      int ch;
      int n;
    
      /* Verify that this file is a valid RLE encoding */
      if ( fgetc ( in ) != IND || fgetc ( in ) != MARK )
        return 0;
    
      while ( ( ch = fgetc ( in ) ) != EOF ) {
        /* If we've found a length encoding */
        if ( ch == IND && isdigit ( ch = fgetc ( in ) ) ) {
          /* Replace the digit we stole */
          ungetc ( ch, in );
    
          /* Get the run length */
          fscanf ( in, "%d", &n );
    
          /* Get the character to print */
          if ( ( ch = fgetc ( in ) ) == EOF )
            return 0;
    
          /* Print n ch's */
          while ( --n >= 0 )
            fputc ( ch, out );
        }
        else {
          /* Just in case. :-) */
          if ( ch != EOF )
            fputc ( ch, out );
        }
      }
    
      return 1;
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    oh. ...

    thanks.

    thats what happens when i read an indepth C text book to try and work things out i guess. :S

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're trying too hard.
    thats what happens when i read an indepth C text book to try and work things out i guess. :S
    It's often times good to get away for a few minutes, clear your head of the problem, and then come back and take a fresh look at it. Some times you can get bogged down trying to think of doing it "one way", and completely miss another much better way to do it, simply because you're hung up on one idea.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C decoder for files encoded in Morse code
    By amsy in forum C Programming
    Replies: 5
    Last Post: 10-22-2004, 12:40 AM
  2. Need RSA encoder / decoder / keygen
    By Rak'kar in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-20-2004, 09:40 PM
  3. Encoder Issue
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2002, 11:34 AM
  4. string encoder / decoder
    By mbell in forum C Programming
    Replies: 0
    Last Post: 09-12-2001, 09:45 PM