6

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.

Community
  • 1
  • 1
Yoshi
  • 63
  • 1
  • 1
  • 3

3 Answers3

7

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.

jonescb
  • 22,013
  • 7
  • 46
  • 42
  • It "messed up the output" because there were no longer any characters on that line to backspace through. Please note that this approach is highly implementation-, console- and terminal- dependant. – Lightness Races in Orbit Apr 05 '11 at 19:19
  • Now, thats a smart solution too! But the only drawback with this approach is the cursor needs to be at the end of the text we want to delete. – Shankar Raju Apr 05 '11 at 19:19
  • For me having the `\n` put the "green fox" on the same line but all the way to the right. I guess my terminal inserts newlines by filling the current line with spaces. If I do more backspaces it moves over to the left more. – jonescb Apr 05 '11 at 19:22
  • Thanks, this is almost exactly what I was looking for. Do you know of a way I can remove or delete the \n char? Or is there a line up char? Or some way I can have the same functionality across lines as you provided in one line? – Yoshi Apr 05 '11 at 19:54
3

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.

jonsca
  • 10,218
  • 26
  • 54
  • 62
  • Thanks. That was similar to many answers I found in various places. I'm rather new at C and programming in general, and the system I have to use isn't very flexible. So at this point I'm trying to develop workarounds to many of the problems. – Yoshi Apr 05 '11 at 19:56
  • For the first example you cited, the selected answer might work, but if you're talking about writing something similar to that text editor you described, backspacing won't be very pleasant. I'm not sure how the libraries themselves do it, but you can look at the source for pdcurses to find out. Good luck with it! – jonsca Apr 05 '11 at 20:32
1

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.

Shankar Raju
  • 4,356
  • 6
  • 33
  • 52
  • he's planning on writing over the same specific text, so clearing the screen will wipe out everything. It would be like a clock display in the corner of the screen where you replace the numbers every second or minute. – jonsca Apr 05 '11 at 19:13
  • 1
    @jonsca, oh i get it now. So, in that case the solution provided by you is a good one :) – Shankar Raju Apr 05 '11 at 19:18