{"id":31133,"date":"2006-05-19T10:00:09","date_gmt":"2006-05-19T10:00:09","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/05\/19\/redirecting-output-can-result-in-altered-program-behavior\/"},"modified":"2006-05-19T10:00:09","modified_gmt":"2006-05-19T10:00:09","slug":"redirecting-output-can-result-in-altered-program-behavior","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060519-09\/?p=31133","title":{"rendered":"Redirecting output can result in altered program behavior"},"content":{"rendered":"<p>\nConsider a program whose output to the console goes like this.\n(I&#8217;ve prefixed each line with the output stream.)\n<\/p>\n<pre>\nstdout: line 1\nstderr: err 1\nstdout: line 2\nstderr: err 2\n<\/pre>\n<p>\nYou want to capture both the normal and error streams,\nso you run the program and append\n&#8220;<code>&gt;output 2&gt;&amp;1<\/code>&#8221; to capture both streams\ninto a single file.\nBut when you look at the resulting output file, you get this:\n<\/p>\n<pre>\nline 1\nline 2\nerr 1\nerr 2\n<\/pre>\n<p>\nWhat happened?\n<\/p>\n<p>\nMost programs\nchange their output behavior depending on whether\nthe output stream is a file or a device.\nIf the output stream is a device (such as the screen),\nthen buffering is disabled and every print statement goes\nto the screen immediately.\nOn the other hand, if the output stream is a file,\nthen buffering is enabled and print statements do not go\ndirectly into the file but rather into an application-managed\nmemory buffer.\nWhen the buffer fills, it is written to the file, and then the\nbuffer is emptied so it can accept more output.\n<\/p>\n<p>\nThis explains the behavior we see above.\nThe program generates its output to both stdout and stderr,\nand the results are buffered.\nWhen the program exits, the buffers are flushed, first\nstdout and then stderr.\nThat&#8217;s why you see all the stdout output grouped together\nand all the stderr output grouped together.\n<\/p>\n<p>\n&#8220;But I don&#8217;t do any of this in my programs; why is it happening anyway?&#8221;\n<\/p>\n<p>\nIf you use the C runtime for your output, then your program does\nbehave this way whether you realize it or not.\nThe default behavior of the C runtimes is to perform unbuffered I\/O\nfor devices and buffered I\/O for files.\nYou can override this behavior in your own programs by calling\n<code>setvbuf<\/code> to force buffering on or off for a file handle.\n<\/p>\n<p>\n(A classmate in college became legendary for fixing a bug\nin one of the programs used in the VLSI class, all of which were\nnotoriously buggy.\nHe patched the binary to disable buffered I\/O.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider a program whose output to the console goes like this. (I&#8217;ve prefixed each line with the output stream.) stdout: line 1 stderr: err 1 stdout: line 2 stderr: err 2 You want to capture both the normal and error streams, so you run the program and append &#8220;&gt;output 2&gt;&amp;1&#8221; to capture both streams into [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-31133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Consider a program whose output to the console goes like this. (I&#8217;ve prefixed each line with the output stream.) stdout: line 1 stderr: err 1 stdout: line 2 stderr: err 2 You want to capture both the normal and error streams, so you run the program and append &#8220;&gt;output 2&gt;&amp;1&#8221; to capture both streams into [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/31133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=31133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/31133\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=31133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=31133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=31133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}