简体   繁体   中英

CLion - Carriage return? \r

I am using the CLion IDE and I am trying to do a carriage return.

I am doing a print statement in C and have the following syntax:

printf("\\rHello World!"); which is inside a loop. The loop still prints every Hello World on its own line. There are no \\n 's in my program. I've tried changing the line separators options to unix mac OS and windows and none of them change the functionality. Google has also led me to no useful answers.

int main()
{
    int i = 0;
    while (i < 5000000)
    {
        printf("\rThis is line number %d!", i++);   
    }

    return 0;
}

My expected output is only a single line of text in the console window.

Thanks.

Your problem is PuTTY console, that is used in CLion by default. You can switch it off in Registry:

Help | Find Action | Registry... =>
run.processes.with.pty [ ] <- uncheck

I recommend you modify the program:

#include <iostream>

int main() {
    int i = 0;
    while (i < 500) {
        printf("\rThis is line number %d!", i++);
        fflush(stdout); // <- add this call
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM