【发布时间】:2013-12-12 08:58:10
【问题描述】:
问题已解决,代码重写如下:
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char** argv){
std::string input;
std::vector<std::string> inputVector;
while(std::getline( std::cin, input ) ){
inputVector.push_back(input);
}
for(int i = 0; i < inputVector.size(); i++){
std::cout << i << " of " << inputVector.size()-1 << " is " << inputVector[i] << "\n";
}
return 0;
}
顺便说一句,CMD 和 Powershell 中的输出在视觉上是不同的 - 在 Powershell 中执行此操作时看起来有两个结束线(也就是说,每个正确的行之间都有一个空行),我怀疑(但尚未调查)这是因为在 Powershell 行的末尾有很多空白,所以当您在前面添加 "xx of xx is " 时,该行会环绕。
=====================ORIGINAL=QUESTION=BENEATH=THIS=LINE=============== =====
这段代码应该只打印所有参数:
//dirparser.cpp
#include <iostream>
int main(int argc, char** argv){
for( int i = 0; i<argc ; i++){
std::cout << i << " of " << argc-1 << " is " << argv[i] << "\n";
}
return 0;
}
而且它似乎运行良好 - 如果我打电话给例如
dirparser.exe a b c
输出如预期:
0 of 3 is dirparser.exe
1 of 3 is a
2 of 3 is b
3 of 3 is c
但是当我这样做时,在命令行中:
dir | dirparser.exe //In CMD
dir | .\dirparser.exe //In Powershell
ls | .\dirparser.exe //In Powershell
我得到的输出是:
0 of 0 is dirparser.exe //CMD
0 of 0 is [directory]\dirparser.exe //Powershell
0 of 0 is [directory]\dirparser.exe //Powershell
没有别的了。
这不是因为dir 和/或ls 什么都不返回——在没有管道的情况下单独调用这些命令会给我像往常一样的文件结构。我怀疑我遗漏了一些重要的东西——可能是关于管道行为——但我不知道应该从哪里开始。
【问题讨论】:
-
管道不传递命令行参数。