的问题已经解决了,代码重写如下:管道和命令行参数
#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
什么都不返回 - 单独调用那些没有管道的命令给我的文件结构照常。我怀疑我错过了一些必不可少的东西 - 可能是关于管道的行为 - 但我对我应该从哪里开始毫无头绪。
管道没有通过命令行参数的管道。 – chris