Thread: trouble with pipes

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Unhappy trouble with pipes

    I wrote a rot13 text filter as my first C++ application. In Windows 2000, I compiled with Borland 5.5. For Linux, I'm using Debian etch for amd64 with bash. All of my I/O is done with iostream cin.getline and cout.

    An optional command line parameter is used to indicate if you want to also apply rot5 to digits. Any other command line parameter gets you the help message.

    In Windows,
    echo testing123 | rot13 r5
    works just as expected

    But in Linux.
    echo testing123 | rot13
    results in the screen being cleared (or maybe just a lot of blank lines) and then nothing. The process sits there using 47% of the CPU. If I type something, what I typed is rot13'ed and the process ends ok. So why isn't the pipe working?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You'd have to show the code to know that. [Obviously not necessarily ALL of the code, but the part that reads data from the input at least, perhaps also the code to set the input up, if you feel that may be part of the puzzle].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Arrow

    Quote Originally Posted by matsp View Post
    You'd have to show the code to know that. [Obviously not necessarily ALL of the code, but the part that reads data from the input at least, perhaps also the code to set the input up, if you feel that may be part of the puzzle].

    --
    Mats
    Code:
    #define MAXBUF 1024
    
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main( int argc, char* argv[] ) {
    	char thisline[MAXBUF];
    	int i,j,rotaction;
    	bool dorot5=false;
    	bool dorot13=false;
    
    	if ( argc == 1 ) {
    		dorot5=true;
    		dorot13=true;
    	}
    	
    /* evaluate command line parameters here */
    	
    	if ( dorot5 == false && dorot13 == false ) {
    		printhelp(); return 0;
    	}
    
    
    	do {
    		cin.getline(thisline,MAXBUF);
    		i = 0;		
    		do {
    
    			if ( thisline[i] == NULL ) break;
    			
    			if ( dorot5 && (int)thisline[i] > 47 && (int)thisline[i] < 53 ) {
    				j = (int)thisline[i]+5;
    				cout << (char)j;
    				i++; continue; };
    
    /* more stanzas like the one above go here */
    
    			cout << thisline[i];
    			i++;
    		} while ( i < MAXBUF+1 );
    	
    		cout << endl;
    	} while ( thisline > "" );
    	
    	return 0;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    thisline[i] == NULL - you cannot compare char to pointer

    do not use "magic numbers" like 47 or 53

    while ( i < MAXBUF+1 ); - you'll access one out of buffer character


    thisline > "" - no meaning of comparing pointer to const char* string

    You should switch from using c-string to C++ string

    PS. Increase your warning level to maximum and PAY ATTENTION to warnings
    Last edited by vart; 02-11-2008 at 03:09 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2009, 06:13 PM
  2. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 01:47 PM
  3. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 03:09 AM
  4. pipes in c
    By ajal1 in forum C Programming
    Replies: 6
    Last Post: 10-29-2005, 03:29 PM
  5. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 06:46 AM