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.

Hatefiend
  • 3,416
  • 6
  • 33
  • 74
  • http://www.keil.com/support/docs/1265.htm Hope that helps – Hellonearthis May 07 '17 at 06:21
  • What OS are you running this on, as the behaviour of the \r may well depend upon the terminal it's appearing on. – Gwyn Evans May 07 '17 at 07:42
  • unhelpful bug here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000221704-Clion-How-to-carriage-return-in-console-prints- – Anona112 Oct 11 '17 at 13:18
  • what if you write `printf("This is line number %d!\r", i++); fflush(stdout);`? – grek40 Oct 11 '17 at 13:36
  • 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 '17 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 '17 at 13:44
  • This might be printf; try `puts("\rfoobar");`. – SIGSTACKFAULT Oct 11 '17 at 20:21
  • 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. – SIGSTACKFAULT Oct 11 '17 at 20:30
  • @Anona112; Which OS you are using? – haccks Oct 14 '17 at 06:58
  • Try compile with a latest C++ compiler and see. Your code is working fine for me while I have compiled with C++ compiler and out put is only one single line. Your current compiler is adding a '\n' character at the end of printf statement. – Abhijit Pritam Dutta Oct 15 '17 at 09:18
  • @haccks Ubuntu 16.10 – Anona112 Oct 15 '17 at 10:06
  • What compiler are you using? `g++ --version` – fundagain Oct 16 '17 at 11:07
  • 2
    Please execute your program from outside of CLion, i.e., from the shell. Do you get the same output? – fundagain Oct 16 '17 at 11:29
  • already did fflush(stdout); g++ (Ubuntu 5.4.0-6ubuntu1~16.04.5); same code behaves well in Terminal, so it must be a CLion thing. – Anona112 Oct 18 '17 at 15:33
  • works now after reboot without any changes. how can I award the 50 to Murphy or God? – Anona112 Oct 18 '17 at 16:04
  • now that's something: doesn't work when Debugging, works when Running! Also printf("\r%d",i) acts as "\r\n" , printf("%d\r",i) ignores the "\r" – Anona112 Oct 20 '17 at 18:42

1 Answers1

7

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;
}
uta
  • 1,451
  • 10
  • 12