的問題已經解決了,代碼重寫如下:管道和命令行參數
#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