1
int main( int argc, char *argv[])
{
    for( count = 0; count < argc; count++ )
    {

         cout << "  argv[" << count << "]" << argv[count] << "\n" <<      endl;           
     }
}

Command $ ls -l | ./main.out

The output will show

Command-line arguments :
argv[0]    ./main.out

My question is, how do I make my program to read the command before that, ls -l

463035818_is_not_a_number
  • 88,680
  • 9
  • 76
  • 150
Lozy
  • 159
  • 4
  • 11

1 Answers1

2

Command line parameters are passed as arguments when calling the program. And your program will read the entire command line arguments.

But What you are doing ($ ls -l | ./main.out) is piping standard output of the command ls -l into the standard input of the program ./main.out.

To read from stdin, do Something like:

 std::string value;
 while(std::getline(std::cin, value)){
       std::cout << value << std::endl;
 }

See Reading piped input with C++ and http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html

Community
  • 1
  • 1
WhiZTiM
  • 20,580
  • 3
  • 39
  • 60