how to print with a while also using a scanf statement - c

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.

Related

ANSI color codes deleting printf

Iam developing a nanoShell, for a school work, and I have a problem with ANSI color codes.
So when I run the program and I write something, and press TAB, then if I backspace my tab I can also delet my previous printf, screenshot below.
I am wondering if exists on C another way to make my printf colored, or if I can block user from pressing TAB.
After the printf, Iam using fgets to read the args introduced, by user.
fgets(readpos, bufSize, stdin);
My printf is: printf("\033[1;31mnanoShell$ \033[0m");
Example:

How to let terminal refresh without printing new stuff in C

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.

Perl (or C printf) back one tab, possible?

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

How do I accept Tab as input from STDIN to autofill text

How do I do autofill a text when Tab key is pressed after a period from STDIN using C?
Input would look like,
C:\>autofil.exe Hello
C:\>autofil.exe Hello. ( When I enter a period, it should autofil Hello after the period)
C:\>autofil.exe Hello.World *World is autofilled when period was entered.
Second requirement is if I am at
C:\>autofil.exe Hello.World (And when i press Tab key, `World` should print with other suggestions, as `Friend`, and if I press Tab again, print `Matt`, and scroll so on when tab is pressed... )
Well, that is my requirement. Tab complete to autofill text. Also, want to know how to read the Tab key from STDIN.
Thanks!
You could use Readline GNU library For Windows, is pretty easy to use, the other way around is to use PDcurses/Ncurses (are almost the same) and do it by hand (handling console behavior and such).
In case you use readline, to acomplish autocomplete is as simple as doing:
rl_bind_key('\t', rl_complete);
char *input = readline("C:\>");
In case you use Ncurses/PDcurses, you will have to do a little more work :)
First you save actual console parameters.
Then you set echo of the input off with noecho().
You will have to handle input and parse the arguments to call programs.
Also you will have to search in dirs (current and the ones on PATH variable) for things matching your current input.
Before ending your program set again the saved console parameters.
Ncurses How-To Is an easy right to the point guide.
Posix curses definition Contains functions and key definitions.

C - Remove and replace printed items

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.

Resources