2

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).

Frank the skank
  • 108
  • 2
  • 10
  • put a /0 character in (last-1)th index of your buffer,may be helpful – Farrokh Jul 26 '14 at 03:36
  • You can find position of newline (see [strstr(s, "\n")](http://www.cplusplus.com/reference/cstring/strstr/)) and truncate string (put '\0' at the end of line (instead of '\n')) – Ilya Jul 26 '14 at 03:37
  • 3
    It would be helpful if the question were edited to indicate how this undesirable newline was emitted. – Mahonri Moriancumer Jul 26 '14 at 03:42
  • Are you talking about the `stdout` buffer, or a memory buffer of your own making? If it's the former, the advice you're gonna get is, "Don't do that". – user3386109 Jul 26 '14 at 03:46
  • @user3386109 It's a `stdout` buffer. – Frank the skank Jul 26 '14 at 04:18
  • Oh, you're talking about terminal emulation (which is generally non-portable). You'll need to specify which operating system/machine type you're using to get detailed help. – user3386109 Jul 26 '14 at 04:23
  • @FrancoSelem, there is no "standard C way" to do what you wish. However, there are ways to do it which are OS and terminal specific. Perhaps you might consider editing your question again and indicate the OS and/or terminal type? (Adding the OS as a tag is a good thing too). – Mahonri Moriancumer Jul 26 '14 at 04:26
  • @MahonriMoriancumer I re-edited the question, sir! – Frank the skank Jul 26 '14 at 04:31
  • @FrancoSelem, The answer you seek is given (below) by icktoofay. – Mahonri Moriancumer Jul 26 '14 at 04:36

3 Answers3

5

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 (e.g., 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 */
icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • Or, to move up 4 lines, `\x1B[4A`. (See: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html ie: `CSI P s A`) – Mahonri Moriancumer Jul 26 '14 at 04:38
  • The answer you gave doesn't work if I wanted to edit the end of a previous line (`"Hello\n" "\x1B[A" "\b "` for instance). Nonetheless, it manages to do what I asked; and for that, I thank you, sir. – Frank the skank Jul 26 '14 at 04:43
1

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.

user3344003
  • 20,574
  • 3
  • 26
  • 62
0

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.