By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

how to clear display

arunmib
100+
P: 104
Hi all, while printing on a monitor, I want to remove the last printed line and replace it with a new string.

My doubt will be better explained with the following example.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.      printf("Hello\n");
  4.      printf("Test");
  5.      return 0;
  6. }
The output of the program will be
Hello
Test


Now, what I want to do is, remove "Hello" from the display and print "Test" in it's place. If I replace the "\n" in the line printf("Hello\n") by "\r", only the first four characters of "Hello" is replaced and the character "o" is left as such and the output is as follows,

Testo

So, how to remove this "o"....
May 3 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 100+
P: 181
You can use clrscr to clear the entire screen before printing next line.

Do you want to clear the entire screen or only specified line
May 3 '07 #2

P: 21
if meant to clear uptil the last line then you can use,
int main()
{
printf( "Hello\n");
system( "cls" ); // calls the cls command.
printf( "Test\n");
return 0;
}
Hope you are looking for this.
May 3 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
Both of those methods rely on on standard features of either the C library or the OS.

But what you could do is put a space on the end of Test

Expand|Select|Wrap|Line Numbers
  1. printf("Hello");
  2. fflush(stdout);
  3. printf("\rTest ");
  4. fflush(stdout);
  5.  
May 3 '07 #4

arunmib
100+
P: 104
In the first point thanks for the reply guys....

to be more precise, i DO NOT want to clear the entire screen, just the current line...

@banfa, those strings were just an example....in real scenario, i would be getting strings of varying length and the "fflush(stdout)" it didn't help me ....

If clearing a specific line is not possible, then i would atleast work in a different way, but i need to make sure and get some opinion from people and some concrete proof...
May 3 '07 #5

Banfa
Expert Mod 5K+
P: 8,916
@banfa, those strings were just an example....in real scenario, i would be getting strings of varying length and the "fflush(stdout)" it didn't help me ....
The fflush is just to make sure that the text is output to the screen because stdout is buffered and it is only required that it is written when a newline character is output.

As to those only being test strings and the real life ones being variable length, just use you noodle. There are plenty of ways of recording how much of the line needs to be blanked or in worst case you can always write and entire line of spaces followed by the new string.
May 3 '07 #6

AdrianH
Expert 100+
P: 1,251
In the first point thanks for the reply guys....

to be more precise, i DO NOT want to clear the entire screen, just the current line...

@banfa, those strings were just an example....in real scenario, i would be getting strings of varying length and the "fflush(stdout)" it didn't help me ....

If clearing a specific line is not possible, then i would atleast work in a different way, but i need to make sure and get some opinion from people and some concrete proof...
Actually, what Banfa stated had only partially to do with fflush(), what he is actully talking about is that you use '\r' to bring the cursor back to the begining of the line and then write over the line, ensuring that you put spaces over those characters that you have not written over. The fflush() is used to ensure the stuff you outputed is done right away. stdout is line buffered so if you do not use fflush() it will not show up until a '\n' is seen or the buffer becomes full.

If you are only replacing part of the end of the line, you can also use '\b' to backspace the number of characters you want to replace. Such as:
Expand|Select|Wrap|Line Numbers
  1. printf("Stuff to complete:  00%");
  2. fflush(stdout);
  3. printf("\b\b\b30%");
  4. fflush(stdout);
  5. printf("\b\b\b60%");
  6. fflush(stdout);
  7. printf("\b\b\b90%");
  8. fflush(stdout);
  9. printf("\b\b\b100%");
  10. printf("\n");
Adrian
May 4 '07 #7

arunmib
100+
P: 104
@banfa, sorry i didn't look at the way you had mentioned. thanks for reply, i think i should be able to work with that....

thanks adrian for making it clear...

I think i can work on from here....
May 4 '07 #8

P: 11
Did you try this?
printf("/r Hello");
I feel this is the simplest way to do that..... I have tried in Turbo C++ (Ver 3.0).


Happy Coding in "C" - Representing the whole COMPUTER
May 4 '07 #9

arunmib
100+
P: 104
Did you try this?
printf("/r Hello");
I feel this is the simplest way to do that..... I have tried in Turbo C++ (Ver 3.0).


Happy Coding in "C" - Representing the whole COMPUTER

I don't think it helps(not using Turbo C++, using DevC++)... no problem, i will around with a logic of clearing the line like Banfa said... Anyway thanks for the reply...
May 7 '07 #10

P: 1
Do you think spaces are the same as an empty line because they aren't visible? They still contribute to line length, etc. Sorry, every character printed to stdout is final. However, terminals interpret codes inside their input that control the buffer, which is what you end up seeing, so look up your terminal capabilities to see what codes are available.

tput cup 0 0
# move cursor to 0,0
printf '%q' "$(tput cup 0 0)"
# $'\033[1;1H' is the code according to tput
printf '%s' $'\e[1;1H'
# (cursor moves to column 0, row 0)

tput normally prints to stdout, which is interpreted by the terminal, but the codes can be captured as above, and they work the same if you send them to stdout from your program. You could find all the codes you need and hard-code them into your program, but it would make more sense to use a third-library library like ncurses to find the terminal's capabilities and sequences. Programs that interpret these codes often provide a way to capture their buffer, the result once these control sequences are removed and used, in case this isn't just about getting the result to look right within your terminal.
Jan 12 '19 #11

Post your reply

Sign in to post your reply or Sign up for a free account.