trouble with pipes

This is a discussion on trouble with pipes within the Linux Programming forums, part of the Platform Specific Boards category; I wrote a rot13 text filter as my first C++ application. In Windows 2000, I compiled with Borland 5.5. For ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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 02:09 PM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21