We all know how to print a string one tab further with \t
printf ("Hello\tthere!\n");
which outputs:
Hello there!
What about printing one tab backwards? I know we can just use \r to start back from the beginning of the line, but there are situation when indentation is more complex than that, I am looking for something like \bt (if it existed) such that I could write:
printf ("Hello\tthere! \n\tHow\n\btare you?\n");
to get in output:
Hello there!
How
are you?
Edit
I am surprised by the amount and speed of replies here, and sometimes even by the uselessness of some comments, but only a few. What I am trying to achieve is of course more complex than this, it's properly formatting an XML file to be easily readable. I wanted to be sure there wasn't an easier way, the response confirmed there wasn't thanks.
Edit 2
Sorry guys, you are right to vote down this question, first because I didn't specify I was writing to a text file, secondly because I was assuming tabs could be "remembered" by the file handler. Sorry for wasting your time.
With printf you control how much tabbing is taking place explicitly. To back tab you would just use fewer tabs. From the example you provided it looks like you are expecting the tab level to be remembering, and have How indented farther than it really will be when printed. It would appear at the same tab stop as there! and are you?
Assuming with:
printf ("Hello\tthere! \n\tHow\n\btare you?\n");
You are expecting to see:
Hello there!
How
are you?
Then just don't add the tab before are you? and you will get what you want. To get fewer tabs, just add fewer \t symbols to your printf. printf doesn't have any memory of what tabbing has occurred before.
printf ("Hello\tthere! \n\tHow\nare you?\n");
No, there is no special character to do what you want. Even the behavior of \b is dependent on the platform.
\bt is simple the backspace character followed by the letter t. So that syntax is supported but there's no special behavior.
But why do you need that here? Why not just use:
printf("Hello\tthere!\r\n\tHow\r\nare you?\n");
There's no way to "back tab", but there are other options:
As you noted, \t gives you a tab forward.
\b can give you a backspace, so you can "back up" with that.
There are also some less-frequently-used ones:
\f is a form feed/new page (eject page from printer)
\v is a vertical tab (move down one line, on the same column)
So using the other options you can simulate what you want.
printf("Hello, this is\n\t\tjust a test.\n");
printf("Hello, this is\v\b\bjust a test.\n");
Ends up looking like:
Hello, this is
just a test.
Hello, this is
just a test.
You could then simplify your code with something like:
#define BACKTAB "\v\b\b"
printf("Hello, this is%sjust a test.\n", BACKTAB);
I think that's the best you can do
Related
I have an odd situation ive made a login system that has many printf and color statements
______________________________________________________________
example border_red(); printf (" $$ ");
_____________________________________________________________
I have multiple printf statements in the menu and in the middle of my code I have a scanf statment to scan the username. The menu gets cut off halfway through making just the top part of the menu appear.
WHAT THE MENU IS SUPPOST TO LOOK LIKE
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ username:type here $
$ $
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
WHAT THE MENU LOOKS LIKE
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ username: type here
i need the entire menu to show when you type in your username
QUESTION: How would i go about printing letters after a scanf statement
char username[50];
printf("#############################");
pritnf("# username:");
scanf(log, & username);
printf(# #');
printf("#############################");
how would i go about not having the scanf statement cut off my menu
Sorry if this is a simple question I may have googled it wrong but ive been at this for 1 hour now and this is the only thing I can turn to.
Both scanf and printf assume line-oriented input and output (think of a hardcopy terminal or teletype). They're just not designed to print a border, then scan from a location within that border1.
If you want to set up borders or place input fields in arbitrary places on screen, you'll have to look beyond the standard C library and use a third-party tool like ncurses.
There are tricks you can use - you can send ANSI terminal escape sequences to place the cursor at arbitrary locations on the screen, but it's ugly and a lot of work. You're better off using a library like ncurses.
I'm trying to use the escape sequence \033[999D as a brute-force way of moving the cursor to the top row in the console. When I run my program, rather than doing what I intend, it returns a left-pointing arrow and a [999D, on the same line that I was last on.
How should I properly use this escape code? Are there any (better) substitutions?
My (test) code:
printf("This is a line\n");
printf("This is another line\n");
printf("\033[999D Overwrite");
My output:
This is a line
This is another line
←[999D Overwrite
Check out the Win32 Console API.
Particularly of interest to you:
GetStdHandle (MSDN)
SetConsoleCursorPosition (MSDN)
Here is an example showing how to move the cursor to a specific position.
Here is an example showing how to clear the screen.
Of interest to people looking to set console colors:
SetConsoleTextAttribute
There are two problems (most likely): The first is that the D VT100 cursor control sequence is to go back a number of columns, which means it will go back a number of columns on the current row. It will not change line.
The second and the likely problem the code is printed is because you probably are using the Windows console program (the "DOS prompt") which is very bad at handling VT100 sequences by default.
Is it possible,somehow,to return to the beginning of the page, not line, page.
Using something like "hello\r" would,obviously, write "hello" and return to the start of the line.
But what about when I am on a second line?
So after "hello\n" it would put me on a second line.
Can I somehow return back to the 1st line.
From the research I have conducted it seems that you can only operate on 1 line, but I am not 100% sure and thus would like someone to confirm this.
Programming in C and using RealTerm.
RealTerm has an ANSI terminal emulation mode.
Find the "Display Formatting " configuration dialog and select the Ansi option.
Now you can embed an escape sequence for Cursor Home much like you embed the newline.
<ESC>[H
Where, of course, <ESC> means the single character 0x1b. You can implement it like this
sprintf(buffer, "%c[Hmy text\n", 27);
transmit(buffer);
or similar.
I want to make a more dynamic interface rather than print out a whole new page every time but do not know how to implement it. For example, if we have a download bar that goes from 0% to 100%, I want to change the number directly on the terminal instead of printing out 100 lines with 1%, 2%, 3%. What should I do with it?
The simpler choice, if you want to keep it on a single line, would be to use \r.
Printing \r will move your cursor to the beginning of the line, giving you the ability to print hover the old characters.
A solution would look something like
for (i = 0; i < 100; ++i)
{
printf("\r%3i%%", i);
fflush(stdout);
/* ... */
}
If you need more advanced control over the terminal, you can use termcaps.
You could print a \r character, which is called the 'carriage-return' and should in most cases return the cursor to the beginning of the line so you can print over the text that's already there. I say should because it's not guaranteed and it depends on the shell the program is running in. If you want to do more exotic stuff you should look into ncurses.
You should use a cross-platform terminal access library, such as libncurses.
I'm writing a program in C compiled in gcc. The question relates to homework, but the specific part I need help with is not part of the homework assignment. A similar question was asked Python - Remove and Replace Printed items, but I'm not using Python code. I've written most of the homework assignment, and now I'm trying to add features.
The first issue I'm trying to do is print out some text onto the screen, then remove that text, then print out new text onto the screen in the same spot where the first text used to be.
For example, I would like the program to print "Quick brown fox", then remove from the screen "brown fox", then print "green fox" where 'brown fox' used to be, so that "Quick green fox" is displayed lastly on screen in the same location
The other issue is to have the program respond to user inputs without using the enter key.
I think these features are possible, since I've run a program called Joe's Own Editor from my system. In it, I can press ctrl-C, which functions like an exit command, and a message is displayed "Lose changes to this file y,n,^C)?" If I then press "n", and only "n", the "Lose cha..." message is removed from the screen and the cursor location is adjusted.
Thanks.
Use the \b (backspace) character.
printf("Quick brown fox");
int i;
for(i=0; i < 9; i++)
{
printf("\b");
}
printf("green fox\n");
I noticed that putting a \n on the first printf() messed up the output.
Doing those console manipulations is dependent on the platform that you are using. You will probably need a library to accomplish what you are trying to do. See something like this which is cross platform, or the old conio library for DOS if you're on Windows.
If I get your question, please try this:
system("cls");
and print a new text on the console.
EDIT:
Also, to answer your second question, have a while loop and:
use getch() found in conio.h
So that you need not wait for the enter key to be pressed as in the scanf.