![](/img/trans.png)
[英]C++: Linux code does not work on Windows - piping data to program
[英]windows piping to a c++ program
我有以下c ++程序:
#include <iostream>
using namespace std;
//will find the last dot and return it's location
char * suffix_location(char *);
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "not enough arguments!" << endl;
for (int i = 0; i < argc; i++)
cout << argv[i] <<endl;
exit(1);
}
//ignore first parameter (program name).
argv ++;
argc --;
//the new suffix
char * new_suffix = argv[0];
argv++;
argc--;
for (int i = 0; i < argc; i++)
{
char * a = suffix_location(argv[i]);
if (a != NULL)
{
a[0] = NULL;
cout << argv[i] << '.' << new_suffix << endl;
}
}
return 0;
}
char * suffix_location(char * file_name)
{
char * ret = NULL;
for (; * file_name; file_name++)
if (*file_name == '.')
ret = file_name;
return ret;
}
我使用以下命令對其進行了編譯:
cl /EHsc switch_suffix.cpp
當我跑步時
switch_suffix py a.exe b.txt
我得到:
a.py
b.py
如預期的那樣。
當我嘗試管道操作時,問題就開始了。 運行以下命令:
dir /B | swich_suffix py
沒有結果,並且運行
dir /B | swich_suffix py
結果:
not enough arguments!
switch_suffix
系統上的管道運行良好-我在其他一些程序上進行了嘗試。
我嘗試創建一個vs項目並從那里編譯代碼-沒有任何幫助。
怎么了,頭可以解決嗎?
我正在使用vs2010工具在win7上運行。
進行管道傳輸時,傳遞給程序的信息位於stdin上,而不是argv上
當您通過管道傳輸到另一個程序時,它將轉到標准輸入,而不是main中的參數。
這段代碼將在stdin
上收到的內容打印到stdout
,請嘗試以下操作:
for (std::string line; std::getline(std::cin, line);) {
std::cout << line << std::endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.