Can someone please explain why the output I get from running a simple C++ program in a Windows console is a bit garbled when it is piped to the more
command.
Here's the code:
#include <iostream>
int main()
{
for(int i=101; i<=120; ++i){
std::cerr << " cerr " << i << std::endl;
if(i%5==0){
std::cout << " cout " << i << std::endl;
}
}
}
Both of these commands produce garbled output after the first few lines. The output differs on each run. Examples are given below:
mycode.exe | more
...
cerr 117
cerr 118
er cout 105
1 cout 110r
1 cout 1159
cerr 120
cout 120
mycode.exe > nul | more
...
cerr 114
cerr 115
6 cerr 11
cerr 117
cerr 118
...
Note that some of the lines have a lot of spaces as you can see from the horizontal scroll bars.
On Linux,
./mycode | more
also gives garbled results but
./mycode > /dev/null | more
does not.
The output without more
is fine for both Windows and Linux:
cerr 101
cerr 102
cerr 103
cerr 104
cerr 105
cout 105
cerr 106
cerr 107
cerr 108
cerr 109
cerr 110
cout 110
cerr 111
cerr 112
cerr 113
cerr 114
cerr 115
cout 115
cerr 116
cerr 117
cerr 118
cerr 119
cerr 120
cout 120
I am using the Visual Studio C++ 2017 Community Edition version 15.7.4 on Windows 10. The program is compiled in 64 bit mode as a Release version.
I think this is easy to reproduce and get the same sort of results whether compiled using the IDE or something like the following:
CL /nologo /D WIN32 /D_WIN32_WINNT=0x502 /EHsc mycode.cpp
- those flags are probably not all needed since it's such a simple program but happen to be included in a bat file I typically use for testing.
As an aside, an example of Linux output using ./mycode | more
is not quite a as bizarre, but it's appears a little odd all the same:
...
cerr 109
cerr 110
cerr cout 110
111
cerr 112
cerr 113
cerr 114
cerr 115
cerr cout 115
116
cerr 117
...