Back to Blog
Console j line7/23/2023 The movement to the beginning of the line. Sequence transmitted to an output device by the system to accomplish It is unspecified whether this character is the exact It is the character designated by '\r' in Start at the beginning of the same physical line in which theĬarriage-return occurred. \r (carriage return) Moves the active position to the initial positionįrom section '3.86 Carriage-Return Character ()' of the POSIX standard ( ):Ī character that in the output stream indicates that printing should The behavior of the carriage return character seems to be platform-dependent.įrom section '5.2.2 Character display semantics' of the C11 standard: Here is an answer for those wondering why carriage return behaves the way it does on their Linux machine. Others have already answered OP's question. You would have to leave an extra line above to allow for the first movement up one line: i = 3 įprintf(fdfile, "Text to erase****************************\n") įprintf(fdfile, "\033[A\33[2KT\rT minus %d seconds.\n", i) Note that if you have something on the line immediately above the line you want to write on, it will get over-written with the first fprintf(). The same result without needing fflush() would require the additional sequence to go up a line: fprintf(fdfile, "\033[A\33[2K\rT minus %d seconds.\n", i) I had to fflush() after each fprintf() because I don't have a new line character at the end '\n'. Note that I used both the \33[2K sequence to erase the line followed by the \r carriage return sequence to reposition the cursor at the beginning of the line. On an implementation note, to get it to work properly for example in a countdown scenario since I wasn't using a new line character '\n'Īt the end of each fprintf(), so I had to fflush() the stream each time (to give you some context, I started xterm using a fork on a linux machine without redirecting stdout, I was just writing to the buffered FILE pointer fdfile with a non-blocking file descriptor I had sitting on the pseudo terminal address which in my case was /dev/pts/21): fprintf(fdfile, "\33[2K\rT minus %d seconds.", i) In xterm specifically, I tried the replies mentioned above and the only way I found to erase the line and start again at the beginning is the sequence (from the comment above posted by as well as and \33[2K\r carriage returns do not include a newline so cursor remains on the same line) but does not erase anything \r brings your cursor to the beginning of the line (r is for carriage return N.B. \033[A moves your cursor up one line, but in the same column i.e. \33[2K erases the entire line your cursor is currently on
0 Comments
Read More
Leave a Reply. |