<p>I have a console app that needs to display the state of items, but rather than having text scroll by like mad I'd rather see the current status keep showing up on the same lines.  For the sake of example:</p>

<blockquote>
  <p><code>Running... nn% complete</code><br>
  <code>Buffer size:  bbbb bytes</code></p>
</blockquote>

<p>should be the output, where 'nn' is the current percentage complete, and 'bbbb' is a buffer size, updated periodically on the same lines of the console.</p>

<p>The first approach I took simply printed the correct number of backspaces to the console before printing the new state, but this has an obnoxious flicker that I want to get rid of.  I also want to stick to either standard library or MS-provided functionality (VC 8) so as not to introduce another dependency for this one simple need.</p>

[{'answer_id': 45292, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 0, 'selected': False, 'text': '<p>In Linux, you can accomplish this by printing \\b and/or \\r to stderr.  You might need to experiment to find the right combination of things in Windows.</p>\n'}, {'answer_id': 45302, 'author': 'Joseph', 'author_id': 2209, 'author_profile': 'https://Stackoverflow.com/users/2209', 'pm_score': 3, 'selected': False, 'text': '<p>If you print using \\r and don\'t use a function that will generate a newline or add \\n to the end, the cursor will go back to the beginning of the line and just print over the next thing you put up.  Generating the complete string before printing might reduce flicker as well.</p>\n\n<p><strong><em>UPDATE</em></strong>: The question has been changed to 2 lines of output instead of 1 which makes my answer no longer complete.  A more complicated approach is likely necessary.  JP has the right idea with the <a href="http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx" rel="nofollow noreferrer">Console API</a>.  I believe the following site details many of the things you will need to accomplish your goal.  The site also mentions that the key to reducing flicker is to render everything offscreen before displaying it.  This is true whenever you are displaying anything on the screen whether it is text or graphics (2D or 3D).</p>\n\n<p><a href="http://www.benryves.com/tutorials/?t=winconsole" rel="nofollow noreferrer">http://www.benryves.com/tutorials/?t=winconsole</a></p>\n'}, {'answer_id': 45316, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': True, 'text': '<p>You can use <a href="http://msdn.microsoft.com/en-us/library/ms686025.aspx" rel="noreferrer">SetConsoleCursorPosition</a>. You\'ll need to call <a href="http://msdn.microsoft.com/en-us/library/ms683231.aspx" rel="noreferrer">GetStdHandle</a> to get a handle to the output buffer.</p>\n'}, {'answer_id': 45317, 'author': 'Johannes Passing', 'author_id': 4372, 'author_profile': 'https://Stackoverflow.com/users/4372', 'pm_score': 2, 'selected': False, 'text': '<p>In case the Joseph\'s suggestion does not give you enough flexibility, have a look at the Console API: <a href="http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx</a>.</p>\n'}, {'answer_id': 49884, 'author': 'Patrick Johnmeyer', 'author_id': 363, 'author_profile': 'https://Stackoverflow.com/users/363', 'pm_score': 3, 'selected': False, 'text': '<p>Joseph, JP, and CodingTheWheel all provided valuable help.</p>\n\n<p>For my simple case, the most straight-forward approach seemed to be based on <a href="https://stackoverflow.com/questions/45286/how-can-i-overwrite-the-same-portion-of-the-console-in-a-windows-native-c-conso#45316">CodingTheWheel\'s answer</a>:</p>\n\n<pre><code>// before entering update loop\nHANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);\nCONSOLE_SCREEN_BUFFER_INFO bufferInfo;\nGetConsoleScreenBufferInfo(h, &amp;bufferInfo);\n\n// update loop\nwhile (updating)\n{\n  // reset the cursor position to where it was each time\n  SetConsoleCursorPosition(h, bufferInfo.dwCursorPosition);\n\n  //...\n  // insert combinations of sprintf, printf, etc. here\n  //...\n}\n</code></pre>\n\n<p>For more complicated problems, the full <a href="http://msdn.microsoft.com/en-us/library/ms682073.aspx" rel="nofollow noreferrer">console API</a> as provided by <a href="https://stackoverflow.com/questions/45286/how-can-i-overwrite-the-same-portion-of-the-console-in-a-windows-native-c-conso#45317">JP\'s answer</a>, in coordination with the examples provided via the <a href="http://www.benryves.com/tutorials/?t=winconsole" rel="nofollow noreferrer">link</a> from <a href="https://stackoverflow.com/questions/45286/how-can-i-overwrite-the-same-portion-of-the-console-in-a-windows-native-c-conso#45302">Joseph\'s answer</a> may prove useful, but I found the work necessary to use <code>CHAR_INFO</code> too tedious for such a simple app.</p>\n'}]
['https://Stackoverflow.com/questions/45286', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/363/']