【问题标题】:How do I read the whole command line?如何阅读整个命令行?
【发布时间】:2023-03-09 16:20:01
【问题描述】:
int main( int argc, char *argv[])
{
    for( count = 0; count < argc; count++ )
    {

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

命令$ ls -l | ./main.out

输出会显示

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

我的问题是,如何让我的程序在此之前读取命令,ls -l

【问题讨论】:

  • AFAIK 你不能。这些参数不会传递给程序。
  • 旁白:如果你想这样做:Don't parse ls.

标签: c++ linux bash command


【解决方案1】:

命令行参数在调用程序时作为参数传递。您的程序将读取整个命令行参数。

但是您正在做的 ($ ls -l | ./main.out) 是将命令 ls -l 的标准输出通过管道传输到程序 ./main.out 的标准输入中。

要读取stdin,请执行 类似的东西:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-09
    • 2014-04-08
    • 2015-05-26
    • 2012-07-16
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多