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
Advertisement
Answer
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