I have a very long for loop, which prints out many lines of information. I would like to keep it to just one line that overwrites previous lines. I tried Console.out.flush() but it does not seem to work.
Asked
Active
Viewed 918 times
1
-
1Most likely you are looking for http://stackoverflow.com/questions/10804233/how-to-modify-the-previous-line-of-console-text – Alexei Levenkov Jun 19 '14 at 17:03
-
Side note: Yang, could you please link to what part of "flush" behavior of the `fflush` method you want to replicate? Not everyone using C# is familiar with C runtime... Also about "keep just one line" - do you mean something like `Console.Clear` or http://stackoverflow.com/questions/8946808/can-console-clear-be-used-to-only-clear-a-line-instead-of-whole-console? – Alexei Levenkov Jun 19 '14 at 17:11
-
These are exactly what I'm looking for. – Yang Jun 19 '14 at 17:38
1 Answers
1
Use
Console.SetCursorPosition
to get where the Cursor is at use
Console.CursorLeft
Console.CursorTop
Example
int i = 0;
Console.WriteLine("Numbers will count below this line");
int cLeft = Console.CursorLeft;
int cTop = Console.CursorTop;
while (true)
{
Console.SetCursorPosition(cLeft, cTop);
Console.WriteLine(i);
i++;
}

Tsukasa
- 6,342
- 16
- 64
- 96
-
Not yet sure if it is answer - consider editing post to clarify what exact interpretation of the question you are answering. Note that I've already linked to exactly the same suggestion - it is not yet clear what OP looking for, otherwise would be duplicate already. – Alexei Levenkov Jun 19 '14 at 17:12
-
1@AlexeiLevenkov Since OP has accepted this answer, I'd say it *is* a duplicate. – jpaugh Jun 15 '17 at 23:06