Theoretically, these two teams should be equivalent:
1
type tmp.txt | test.exe
2
test.exe < tmp.txt
I have a process involving # 1 that has worked just fine for many years; at some point over the past year, we started compiling a program with a newer version of Visual Studio, and now it fails due to incorrect input (see below). But No. 2 is successful (without exception, and we see the expected result). Why will # 2 succeed if # 1 fails?
I was able to reduce test.exe in the program below. Our input file has exactly one tab per line and evenly uses CR / LF line endings. Therefore, this program should never write to stderr:
#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; }
When starting through # 1, I get the following output with the same numbers every time (in each case, this is because getline returns two concatenated strings without an intermediate line); when running through # 2 (correctly) there is no output:
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.
source share