简体   繁体   中英

How do I read the whole command line?

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

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

Command $ ls -l | ./main.out $ 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM