在 C 中使用转义序列 \r 后,如何将光标移至行尾(不影响行的内容)?

How to take to cursor to the end of the line (Without disturbing the content of the line) after we use the escape sequence \r in C?

我想知道如何将光标移动到行尾而不覆盖该行的内容:

例如:

#include <stdio.h>

int main(void)
{
    printf("This is a test line\r");
    printf("###"); // Cursor moves to start of the line and over writes the content
    printf("I want to print this at the end of the line"); // I want to take the cursor to the end of the line and then print this
    return 0;
}

输出:

###I want to print this at the end of the line

但我希望输出是这样的(通过光标移动,而不是简单地在 printf() 中使用它):

###s is a test lineI want to print this at the end of the line

在 C 中执行此操作的方法是什么?

使用 ANSI escape sequences 适合我

#include <stdio.h>

int main(void) {
    printf("One Two Three\x1b[s\r");
    //                   ^^^^^^      ESC[s ==> save cursor position
    printf("###\x1b[u");
    //         ^^^^^^                ESC[u ==> restore cursor position
    printf("FOUR FIVE SIX\n");
}

表观输出

### Two ThreeFOUR FIVE SIX
#include <stdio.h>
        
        int main()
        {
            printf("This is a test line");
            printf("I want to print this at the end of the line\r");
            printf("###");
             
            return 0;
        }

输出: ###s is a test lineI want to print this at the end of the line