【发布时间】:2016-04-21 23:00:32
【问题描述】:
理论上,这两个命令行应该是等价的:
1
type tmp.txt | test.exe
2
test.exe < tmp.txt
我有一个涉及#1 的流程,多年来一直运行良好;在去年的某个时候,我们开始使用更新版本的 Visual Studio 编译程序,但现在由于输入格式错误而失败(见下文)。但是#2 成功了(没有例外,我们看到了预期的输出)。为什么在 #1 失败的情况下 #2 会成功?
我已经能够将 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.
【问题讨论】:
-
我认为现在这算最小和完整。要进行验证,我需要发布确切的输入文件,但我看不到在此处附加文件的方法。
-
您可以为生成输入文件的程序提供源代码,例如here's an attempt to demonstrate the same issue in Python
标签: windows command-line