12

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.

16
  • keil.com/support/docs/1265.htm Hope that helps May 7, 2017 at 6:21
  • 1
    Just for grins, it might be interesting to write your output to a file or memory block and then inspect the result to see if perhaps an \n is being introduced unexpectedly by the compiler...or perhaps translating the \r into an \r\n combination...
    – David W
    Oct 11, 2017 at 13:37
  • 1
    It's completely possible that the cross-platform clion is "supporting" the oldschool apple users who have CR as newline character.
    – grek40
    Oct 11, 2017 at 13:44
  • 3
    For me, on WSL, \r causes the cursor to go to the start of the line without going to a new line (akin to pressing Home in Word) and overwrites the old line. Oct 11, 2017 at 20:30
  • 2
    Please execute your program from outside of CLion, i.e., from the shell. Do you get the same output?
    – fundagain
    Oct 16, 2017 at 11:29

1 Answer 1

7
+50

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;
}
4
  • Related issue in the tracker youtrack.jetbrains.com/issue/CPP-6949
    – uta
    Oct 17, 2017 at 22:29
  • I'll give this a try very soon. Thanks!
    – Hatefiend
    Oct 18, 2017 at 0:10
  • Problem reappeared. In Debug only!
    – Anona112
    Oct 20, 2017 at 18:35
  • You need set up the Registry for each CLion re-run. In the case of Debug, your program get the console from debugger and it is different story. It depends heavily on the debugger, OS and settings.
    – uta
    Oct 21, 2017 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.