简体   繁体   中英

C - Remove a newline character that has already been printed

How can I remove a newline character that has already been printed and loaded onto the buffer? As far as I know, the \b character only moves the cursor back in the current line, and doesn't work with newline characters. How can I get around this?

It would be helpful if the question were edited to indicate how this undesirable newline was emitted. – Mahonri Moriancumer

It is actually not undesirable. I want to preview the user with one state of the buffer, before outputting the other one. I do not however, want to continuously display multiple buffer-states. For instance, say I want to display two matrices,

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

and

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

I want the second matrix to replace the first one. To do this, I need to remove the first matrix's outputted numbers.

Perhaps you might consider editing your question again and indicating the OS and/or terminal type?

Currently running Linux (Ubuntu) x64, using xterm (ROXTerm to be exact).

There are two widely-supported characters for writing over things that have been written earlier: \\b , backspace, as you mentioned, and \\r , carriage return, to bring the 'write head' back to the start of the line.

If you want to go back to a previous line, you're not completely out of luck, but you'll have to resort to something that, while still widely supported, particularly by terminal emulators, may not be quite so widely supported (eg, it probably wouldn't work if you were to pipe it to lp ). I am referring to ANSI escape sequences. There's plenty, but of note for you is ^[[A , which moves the 'write head' up one line. ( ^[ here is the ESC ASCII character, hex value 0x1B ). To use it in C:

printf("\x1B[A");  /* move up one line */

The solution to this problem depends upon the type of output device you are using and system. It sounds like you are using a console. If you are using a Unix variant, you could use the curses library to control the screen and position the text where you want it.

The problem you are facing is that \\n is a C construct. At some point the system converts \\n to a sequence for a console display.

I'm posting this to help anyone who comes here from google.

You can use the ANSI escape sequence \x1B[s to save the cursor position at the end of a line that you want to overwrite, then use a \x1B[u\x1B[<number of lines to move up>A to put your cursor back to where it was before the newline character.

Here's a working example that overwrites the previous line AFTER the user presses enter:

#include <stdio.h>

int main() {
  char user_input[2];

  printf("Press enter to see a magic trick -> \x1B[s");
  fgets(user_input, 2, stdin); //press enter to continue
  
  printf("\x1B[u\x1B[ATa-da!\n\nLook at line 1!\n");
  fflush(stdout); //flush output to console immediately
  return 0;
}

It should be noted that not all terminals support all ANSI escape sequences. You can view your terminal's capabilities with man 5 terminfo .

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