如何阅读整个命令行?
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;
}
}
命令$ ls -l | ./main.out
输出将显示
Command-line arguments :
argv[0] ./main.out
我的问题是,如何让我的程序读取之前的命令,ls -l
命令行参数在调用程序时作为参数传递。您的程序将读取整个命令行参数。
但是您正在做的 ($ 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
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
命令行参数在调用程序时作为参数传递。您的程序将读取整个命令行参数。
但是您正在做的 ($ 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