Renjith G
Renjith G

Reputation: 4878

How to go to the previous line in a C code

If for the following code:

printf("HEllo\n");    // do not change this line.
printf("\b\bworld");

I need an output: Helloworld (In a single line). But this does not work fine. Could anyone please explain the reason? And other escape sequence if any.

Upvotes: 4

Views: 45129

Answers (7)

Project Zero
Project Zero

Reputation: 63

You can do it actually in a platform-independent* way, assuming that you can somehow calculate the x offset of the previous line.

int x = printf("Hello, World!\n");
gotoxy(x-1-1,gety()-1); // One for the length-offset difference and the other to skip \n
printf("\b \b");

You can avoid using that variable by directly replacing it with x.

Note: printf() returns an int (the length of the passed String (of characters)). Use it wisely :)

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244722

How about simply:

printf("Helloworld");

\n is an escape sequence for a new line. Since you want everything to appear on the same line, there's no reason to specify \n.


The problem is you can't reliably move back up a line (using \b) after you've printed a new line. But if you require that there be two lines of code in your source, you can simply omit both escape sequences:

printf("HEllo");
printf("world");


If you're writing a Win32 console application, you can take advantage of the Console Screen Buffer API. The following code will move 1 line up:

printf("HEllo\n");

CONSOLE_SCREEN_BUFFER_INFO coninfo;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &coninfo);
coninfo.dwCursorPosition.Y -= 1;    // move up one line
coninfo.dwCursorPosition.X += 5;    // move to the right the length of the word
SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition);

printf("world");

Output:

HElloworld

Upvotes: 7

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43088

Remove "\n" from your first printf. It moves the cursor to a new line.

Here is the list of escape sequences.

If you can't remove "\n", then you can do make a copy of a substring without these charaters. See the following example:

  const char* from = "12345678";
  char *to = (char*) malloc(6);
  strncpy(to, from+2, 5);

All you need is to determine the index of "\n" characters.

Upvotes: 4

Liviu Mandras
Liviu Mandras

Reputation: 6617

It is because '\b' is a terminal escape code... (sans the 'escape' of course ;-)

so it only modifies what you see on the terminal. That is, the terminal adjusts its display to respond to the backspace code, even though it received everything prior to it. A file also receives everything, including the backspace code, but it is not a tty device so the file's content is not modified; it keeps everything you send to it.

If printing an extra blank is a problem, then you should code some extra logic to print the trailing blank on every output except the last.

Reference This is in reference to files but maybe same idea applies. You might want to check out this link there is a very detailed explanation that most probably answers your question.

Upvotes: 1

ehudokai
ehudokai

Reputation: 1926

In a console, you can't go up a line. You can clear the screen and redraw it (which simulate going up a line.) Or you can rewrite on the same line. \r will take you to the beginning of the line you just printed.

Upvotes: 0

tdammers
tdammers

Reputation: 20721

The backspace character, when sent to a stream (such as through the printf() family of functions), does not seek backward in the file, it is sent as-is. If you run your example, the backspace character will be output as "garbage".

If you don't want a new line, don't post a newline character.

Upvotes: 1

templatetypedef
templatetypedef

Reputation: 372724

There is no platform-independent control character to move back up a line. This harkens back to the days of line printers, where printf actually would print a line of text onto a sheet of paper and there was no way of retracting the paper to overwrite what had already been printed.

That said, there are libraries like ncurses that let you move the cursor around the console. They're just not part of the standard libraries.

Upvotes: 7

Related Questions