從理論上講,這兩個命令行應該是等價的:爲什麼重定向會在管道出現故障的地方工作?
type tmp.txt | test.exe
test.exe < tmp.txt
我有涉及#1的過程,對於很多年了,工作就好了;在去年的某個時候,我們開始用更新版本的Visual Studio編譯程序,並且由於輸入格式錯誤(見下文),它現在失敗了。但#2成功(沒有例外,我們看到預期的輸出)。爲什麼#2會在#1失敗的地方成功?
我已經能夠將test.exe降低到以下程序。我們的輸入文件每行只有一個標籤,並統一使用CR/LF行尾。所以這個方案不應該寫標準錯誤:
#include <iostream>
#include <string>
int __cdecl main(int argc, char** argv)
{
std::istream* pIs = &std::cin;
std::string line;
int lines = 0;
while (!(pIs->eof()))
{
if (!std::getline(*pIs, line))
{
break;
}
const char* pLine = line.c_str();
int tabs = 0;
while (pLine)
{
pLine = strchr(pLine, '\t');
if (pLine)
{
// move past the tab
pLine++;
tabs++;
}
}
if (tabs > 1)
{
std::cerr << "We lost a linebreak after " << lines << " good lines.\n";
lines = -1;
}
lines++;
}
return 0;
}
當通過#1運行,我得到下面的輸出,同一個號碼,每一次(在每種情況下,這是因爲函數getline返回兩個級聯線中間沒有越線);當通過#2運行,有(正確地)無輸出:
We lost a linebreak after 8977 good lines.
We lost a linebreak after 1468 good lines.
We lost a linebreak after 20985 good lines.
We lost a linebreak after 6982 good lines.
We lost a linebreak after 1150 good lines.
We lost a linebreak after 276 good lines.
We lost a linebreak after 12076 good lines.
We lost a linebreak after 2072 good lines.
We lost a linebreak after 4576 good lines.
We lost a linebreak after 401 good lines.
We lost a linebreak after 6428 good lines.
We lost a linebreak after 7228 good lines.
We lost a linebreak after 931 good lines.
We lost a linebreak after 1240 good lines.
We lost a linebreak after 2432 good lines.
We lost a linebreak after 553 good lines.
We lost a linebreak after 6550 good lines.
We lost a linebreak after 1591 good lines.
We lost a linebreak after 55 good lines.
We lost a linebreak after 2428 good lines.
We lost a linebreak after 1475 good lines.
We lost a linebreak after 3866 good lines.
We lost a linebreak after 3000 good lines.
我認爲這個算作現在最小和最完整。爲了驗證,我需要發佈確切的輸入文件,但我沒有看到在這裏附加文件的方法。 –
你可以提供一個程序的源代碼來生成輸入文件,例如[這裏試圖用Python演示相同的問題](https://gist.github.com/zed/dd44ade13d313ceb8ba8e384ba1ff1ac) – jfs