How to go to the previous line in a C code

后端 未结 7 2072
渐次进展
渐次进展 2021-01-14 11:14

If for the following code:

printf(\"HEllo\\n\");    // do not change this line.
printf(\"\\b\\bworld\");

I need an output: Helloworld (In a

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 11:49

    How about simply:

    printf("Helloworld");
    

    \n is an escape sequence for a new line. Since you want everything to appear on the same line, there's no reason to specify \n.


    The problem is you can't reliably move back up a line (using \b) after you've printed a new line. But if you require that there be two lines of code in your source, you can simply omit both escape sequences:

    printf("HEllo");
    printf("world");
    


    If you're writing a Win32 console application, you can take advantage of the Console Screen Buffer API. The following code will move 1 line up:

    printf("HEllo\n");
    
    CONSOLE_SCREEN_BUFFER_INFO coninfo;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &coninfo);
    coninfo.dwCursorPosition.Y -= 1;    // move up one line
    coninfo.dwCursorPosition.X += 5;    // move to the right the length of the word
    SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition);
    
    printf("world");
    

    Output:

    HElloworld

提交回复
热议问题