Microsoft KB Archive/122442

= Unexpected output in your console application when the standard output and error streams are redirected or piped =

Article ID: 122442

Article Last Modified on 8/5/2005

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q122442

<div class="symptoms_section">

SYMPTOMS
When the standard output and error streams are redirected or piped, console application output may not be serialized. This can result in unexpected output.

<div class="cause_section">

CAUSE
Although devices as serialized by default, the current Microsoft CRT implementation does not treat pipes as if they were devices (due to the potentially high overhead--added code--of doing so.) Thus, if stderr and stdout refer to the same file or device, and output is redirected with a pipe, the output will not be serialized.

<div class="status_section">

STATUS
This behavior is by design. The potential for slow execution currently outweighs the effort necessary to implement pipes such that they are treated as devices.

<div class="moreinformation_section">

MORE INFORMATION
To demonstrate this behavior, build and run the sample code (TEST.C) below. If it is executed from an MS-DOS or Windows NT command prompt, the following output is generated: <pre class="fixed_text"> c:\>test hello world (stdout) hello world (stderr) hello world (stdout) hello world (stderr) However, if the output is piped with the MORE command, it will not be serialized, as follows: <pre class="fixed_text"> c:\>test | more hello world (stderr) hello world (stderr) hello world (stdout) hello world (stdout)

Sample code
/* Compile options needed: none /*

/* TEST.C */

#include <stdio.h>

int main(void) {    printf ( "Hello world (stdout)\n" ); fprintf ( stderr, "Hello world (stderr)\n" ); printf ( "Hello world (stdout)\n" ); fprintf ( stderr, "Hello world (stderr)\n" ); }

Keywords: kbtshoot kbprb KB122442

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.