C - Remove and replace printed items - c

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.

Related

Trouble with Scilab "print" instruction

Trying to debug a program in Scilab, I inserted a couple
of "print" instructions to track what is going on.
At first, I used the %io(2) output "file" which, according
to the Help, stands for the console. Nothing happened.
Then I used an actual filename:
print("C:\Leszek\Xprmnt\scl\Ovsjanko\K3ScilabLog.txt", "START! \n \n \n \n GOING \n")
which does print to that file, but when the dust has settled
and I want to inspect the file what I find inside is just the last
message (just before the program crashed), even though there should
have been others before it, including the "START" etc in the quote above.
Apparently, every print command reopens the file for writing as a clean slate,
overwriting whatever was in it before. Looking into Help and online docs
I didn't find any options or parameters that I could use to change this.
What I want is, obviously, the output from all my print commands since the
beginning of the program, either on the console or in a text file.
TIA.
Just use disp("some text") or mprintf("format",var1,...,varn), both will display on the console. If you need to write in a file use fd = mopen("file.txt") then mfprint(fd,"format",var1,...,varn).

how to print with a while also using a scanf statement

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.

Displaying text files

I'm trying to display the "Phrack" text files. The problem is that the screen doesn't clear before displaying the text file. And overwrites whatever is on the screen at the time. I've tried printf() declarations, like printf("^[[2J") and printf("^[[22;1H") and so forth. And various ncurses "clear screen" commands. None of which worked. Here's the line:
system("/usr/bin/stty -raw") | system("/usr/bin/cat /home/imp/phrack/1/P01-01") | system("/usr/bin/stty -cooked");
Thanks.
The line
printf("^[[2J")
and the tag c indicate that OP wants to write a program in C to clear the screen. The problem with that line is that there's no escape character. This would work:
printf("\033[H\033[2J"); fflush(stdout);
because it uses the escape character. I added the fflush to make it happen "now" rather than sometime later.
There are no uses of ncurses in the question.

UART, return to the start of the page

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.

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

Resources