Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,492 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,302 people online right now. Registration is fast and FREE... Join Now!




Implementing indirection

 
Reply to this topicStart new topic

Implementing indirection, Outputting to stdout when indirection is used

dr_worm
1 Mar, 2007 - 07:39 PM
Post #1

D.I.C Head
**

Joined: 11 Sep, 2006
Posts: 131


My Contributions
I've got this program that looks like this:

CODE

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char convert( char );

int main () {

  char line[96], *line_ptr, output[96];
  int index, lines_processed, punct_chars;
  lines_processed = 0;
  punct_chars = 0;

  for( lines_processed = 0; 1; ++lines_processed ) {

    printf( "Input a line> " );
    line_ptr = fgets( line, 96, stdin );
    if( line_ptr != line || line_ptr[0] == EOF ) break;
    for( index = 0; index < 96; ++index ) {
      if( ispunct( (int)line_ptr[index] ) ) ++punct_chars;
      output[index] = convert( line_ptr[index] );}
    printf( "%s%s", line_ptr, output );
  }

  printf( "\n%d lines processed, %d punctuation characters.\n", lines_processed, punct_chars );

  return 0;
}

char convert( char input ) {

  if( ((int)input >= 'A' && (int)input < 'Y') || ((int)input >= 'a' && (int)input < 'y') ) { input+= 2;}
  else if( (int)input == 'Y' || (int)input == 'Z' || (int)input == 'y' || (int)input == 'z' ) { input-= 24;}
  
  if( (int)input == '(' ) { input+= 1;}
  else if( (int)input == ')' ) { input-= 1;}
  
  if( (int)input == '[' || (int)input == '{' || (int)input == '<' ) { input+= 2;}
  else if( (int)input == ']' || (int)input == '}' || (int)input == '>' ) { input-= 2;}

  return input;
}


My TA is going to use indirection to input a sample code that has the opposite of what my code will do to it, done to it (if my code is a ROT2 encryption, his is written is ROT-2). So it'll look something like this:

CODE
./my-code < his-code.c


He wants the code output to stdout (his file) so he can compile it and see if it actually works. It obviously reads from stdin through indirection, but how do I get what would normally output to the screen (stdout) to output to the file through indirection?

For example, if I do this:

CODE
./my-code < his-code.c > his-code.c


All I get output is the last line printed to the screen in the main function, the "number of lines processed" line, in his-code.c. I need the whole converted code to actually compile. Any help?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Implementing Indirection
2 Mar, 2007 - 12:25 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well on windows you program runs and seems to be on task except that it prints all the prompt lines come out in the output.

The basic "shell" for a filter program is:

CODE
#include <stdio.h>
int main()
{
    char c;
    while ((c=getchar()) != EOF)
    {
        putchar(convert(c));
    }
    return 0;
}


Note that it does not print out anything other than the intended output, and it does not use an input function which will "echo" the input.

BTW: A very good environment to write these kinds of programs is Perl. Don't get me wrong I love C, but processing strings can get to be a pain. Lets just say there was a reason that Larry Wall invented Perl.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:41PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month