本文介绍了为什么重定向在管道失效的地方工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 理论上,这两个命令行应该是等效的: 1 code> type tmp.txt | test.exe 2 test.exe< tmp.txt 我有一个涉及#1的过程,多年来,在过去一年里的某个时刻,我们开始用较新版本的Visual Studio编译程序,并且由于格式不正确的输入而失败(见下文)。但是#2成功(没有异常,我们看到预期的输出)。为什么#2成功,#1失败? 我已经能够减少test.exe到下面的程序。我们的输入文件每行只有一个制表符,并统一使用CR / LF行尾。所以这个程序不应该写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)) { ; } const char * pLine = line.c_str(); int tabs = 0; while(pLine) { pLine = strchr(pLine,'\t'); if(pLine) { //移过标签 pLine ++; tabs ++; } } if(tabs> 1) { std :: cerr< 我们在<线<< good lines.\\\; lines = -1; } lines ++; } return 0; } 当通过#1运行时,我得到以下输出,每次(在每种情况下,这是因为getline已经返回两个连接的行,没有中断linebreak);当通过#2运行时,有(正确)没有输出: 我们在1468个好线后失去了一个linebreak。 我们在20985个好线后输了一个linebreak。 我们在6982个好线后失去了linebreak。 我们在1150个好线后失去了linebreak。 我们在276条好线后失去了linebreak。 我们在12076个好线后输了一个linebreak。 我们在2072个好线后失去了linebreak。 我们在4576条好线后失去了linebreak。 我们在401个好线后失去了linebreak。 我们在6428条好线后失去了linebreak。 我们在7228个好线后失去了linebreak。 我们在931个好线后失去了linebreak。 我们在1240个好线后失去了一个linebreak。 我们在2432个好线后失去了一个linebreak。 我们在553条好线后失去了linebreak。 我们失去了一个linebreak后6550好线。 我们在1591个好线后输了一个linebreak。 我们在55条好线后失去了linebreak。 我们在2428线路后失去了linebreak。 我们在1475条好线后输了一个换线。 我们在3866个好线后输了一个linebreak。 我们在3000条好线后失去了linebreak。 解决方案 https://connect.microsoft.com/VisualStudio/feedback/details/1902345/regression-fread-on-a-pipe-drops-some-newlinesrel =nofollow>已知问题: In theory, these two command-lines should be equivalent:1type tmp.txt | test.exe2test.exe < tmp.txtI have a process involving #1 that, for many years, worked just fine; at some point within the last year, we started to compile the program with a newer version of Visual Studio, and it now fails due to malformed input (see below). But #2 succeeds (no exception and we see expected output). Why would #2 succeed where #1 fails?I've been able to reduce test.exe to the program below. Our input file has exactly one tab per line and uniformly uses CR/LF line endings. So 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 run via #1, I get the following output, with the same numbers every time (in each case, it's because getline has returned two concatenated lines with no intervening linebreak); when run via #2, there's (correctly) 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. 解决方案 This turns out to be a known issue: 这篇关于为什么重定向在管道失效的地方工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 23:43