Results 1 to 5 of 5

Thread: Pipe output and input to c++ program

  1. #1
    Join Date
    Sep 2005
    Beans
    1,596
    Distro
    Ubuntu Mate 20.04 Focal Fossa

    Pipe output and input to c++ program

    I'm a little confused here, here's what I'm trying to accomplish. I made a small app to try things like this out to see if it worked but it didn't. I created an application called lcase which converts strings to lowercase and even removes spaces - basically it takes in the string, puts it in a vector, outputs the string, but if an option is entered it removes the spaces via cout << arg[x] << " ";

    Here's what i want to be able to do:

    Code:
    #hello.txt
    Hello World!
    #/hello.txt
    
    $ cat hello.txt | lcase
    hello world!
    $ cat hello.txt | lcase -l
    helloworld!
    Even in reverse I'd like to do:
    Code:
    lcase -l | cat hello.txt
    helloworld!
    lcase | cat hello.txt
    hello world!
    It works if I do this:

    Code:
    lcase `cat hello.txt`
    But that's not how I want it to work. Do I need to create pipes or read from stdout or what do I do...
    MBA M1 - M1 8GB 256GB - macOS Monterey
    MSI GL65 - i5-10300H 16GB 512GB GTX 1650 Windows 11
    Galaxy Book Go - Snapdragon 7c Gen 2 4GB 128GB Windows 11

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Pipe output and input to c++ program

    You should be able to do the following:
    Code:
    $ cat hello.txt | lcase
    hello world!
    
    $ cat hello.txt | lcase -l
    helloworld!
    
    $ lcase < hello.txt
    hello world!
    
    $ lcase -l < hello.txt
    helloworld!
    
    $ lcase `cat hello.txt`
    hello world!
    
    $ lcase -l `cat hello.txt`
    helloworld!
    If your program is not supporting these features, then perhaps it is not reading from standard-in (std::cin), but instead merely analyzing the command line arguments using argc/argv.

    Why not post your code so we can see what you are doing?

    P.S. You do not require a vector to accomplish the results shown above.
    Last edited by dwhitney67; May 11th, 2014 at 01:04 AM.

  3. #3
    Join Date
    Sep 2005
    Beans
    1,596
    Distro
    Ubuntu Mate 20.04 Focal Fossa

    Re: Pipe output and input to c++ program

    I'm addicted to vectors, don't ask why, I know they take up a bit a memory (and I keep forgetting to clear them), but here's my code:
    Code:
    #include <iostream>
    //#include <stdio.h>
    #include <string>
    #include <vector>
    #include <locale>
    
    using namespace std;
    
    string makeLowerCase(string x)
    {
    	locale loc;
    	string ret = "";
    	for(string::size_type i =0; i<x.length(); i++)
    		ret += tolower(x[i], loc);
    	return ret;
    }
    
    int main(int argv, char* argc[])
    {
    	bool combine = false;
    	vector <string> arglist;
    	unsigned int x;
    	for(x = 1; x < argv; x++){
    		string arg = argc[x];
    		if(arg == "-l"){
    			combine = true;
    		}else {
    			arglist.push_back(arg);
    		}
    	}
    	for(x = 0; x < arglist.size(); x++) {
    		if(combine)
    //			printf("%s", arglist[x].c_str());
    			cout << makeLowerCase(arglist[x]);
    		else
    //			printf("%s ", arglist[x].c_str());
    			cout << makeLowerCase(arglist[x]) << " ";
    	}
    	cout << "\n";
    	return 0;
    }
    MBA M1 - M1 8GB 256GB - macOS Monterey
    MSI GL65 - i5-10300H 16GB 512GB GTX 1650 Windows 11
    Galaxy Book Go - Snapdragon 7c Gen 2 4GB 128GB Windows 11

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Pipe output and input to c++ program

    Ok, now I can what you are doing.

    A few comments:

    1. You can name the parameters in the main() function anything you want, but traditionally the int parameter is named 'argc' (argument count) and the char pointer array is named 'argv' (argument variables or vector). There's also a third parameter (that most programmers rarely use) called 'envp', which is also a char pointer array, and contains environment variables.

    2. On line 23 of your code, you are comparing an unsigned int with an int (argv).

    3. Your program does not support quoted input (e.g. "Hello World") in cases where the white-space must be stripped away.
    Code:
    $ ./lcase "Hello World"      # This one is ok
    hello world
    
    $ ./lcase -l "Hello World"   # This one does not work
    hello world
    As for receiving data via pipes, there are bash shell types that I indicated earlier. One involves using the vertical bar, which is typically referred to as the pipe symbol (that is |), and the other is the standard-in redirection symbol, or the less-than character <.

    The only way to receive data using these types of constructs is to read from standard-in. Your program will need to determine whether it is going to process exclusively the command-line arguments (via argc and argv), or read from standard-in. If the latter, there may be one argument passed to you via argc/argv, and that is the -l option.

    Note, after you have augmented your program to support reading from standard-in, you should be able to test it like this:
    Code:
    $ ./lcase
    Hello World      <--- Your input
    hello world      <--- app output
    <ctrl-d>
    
    $ ./lcase -l
    Hello World      <--- Your input
    helloworld       <--- app output
    <ctrl-d>
    P.S. In lieu of the vector, consider combining argc/argv input into a space-delimited string. For example:
    Code:
    std::string input;
    int start;
    
    //TODO:  Determine if start should be set to 1 or 2, depending whether the -l option is provided.  Or consider using getopt().
    
    // Combine all cmd-line input into one large string.
    for (int i = start; i < argc; ++i)
    {
        input += argv[i];
        input += " ";
    }
    Last edited by dwhitney67; May 11th, 2014 at 12:42 PM.

  5. #5
    Join Date
    Sep 2005
    Beans
    1,596
    Distro
    Ubuntu Mate 20.04 Focal Fossa

    Re: Pipe output and input to c++ program

    Gotcha, thanks! =D
    MBA M1 - M1 8GB 256GB - macOS Monterey
    MSI GL65 - i5-10300H 16GB 512GB GTX 1650 Windows 11
    Galaxy Book Go - Snapdragon 7c Gen 2 4GB 128GB Windows 11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •