13

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 Commented 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... Commented 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. Commented Oct 11, 2017 at 13:44
  • 4
    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. Commented 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? Commented 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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Related issue in the tracker youtrack.jetbrains.com/issue/CPP-6949
I'll give this a try very soon. Thanks!
Problem reappeared. In Debug only!
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.