473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to clear display

arunmib
104 100+
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
10 13412
svlsr2000
181 Expert 100+
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
ajayraj
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
9,065 Expert Mod 8TB
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
104 100+
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
9,065 Expert Mod 8TB
@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
1,251 Expert 1GB
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
104 100+
@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
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
104 100+
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
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

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

Similar topics

6
by: Maxd out | last post by:
Hi I'm wondering if you can clear a monitor display cout that asks for something after it's been read in by cin e.g; cout <<"Enter number" <<endl; If so how can it be done?
0
by: Olly | last post by:
I am using the following pure css layout with a header and footer: http://www.fu2k.org/alex/css/layouts/3Col_NN4_FMFM.mhtml The left column floats to the left of the main content (middle...
0
by: stingrays | last post by:
Below is my code for a combobox. Basically, I have 2 comboboxes. 1 box is the application selection and the other retrieves data from the dataset (SQL Server) to populate the drop down in the...
5
by: Davie | last post by:
I'm developing an application on the smartphone. To reduce the size of the application i'm using panels where appropriate instead of forms. As a result of the design, I need to have different...
3
by: Northern | last post by:
I need to write code clear the display box of my DropDownList (something like clear current selected item) while still keep the loaded item list in the DropDownList. I tried DropDownList's...
3
by: Wayne | last post by:
I downloaded the Office 2007 Beta today and have been clicking around Access and the other Office Apps. They all have clear type switched on despite the fact that I have it turned off in Windows. ...
4
by: Dave | last post by:
TIA for the help.....this should be easy for a pro....I need the two divs with text to display on the same line at the top of the container..??? <html> <head> <style> body...
2
by: nickyeng | last post by:
i have a function that clear the screen and then display array to cout. i compiled no error no warning, i run it, the screen really get "clear" but it does not display anything to cout, and it...
0
by: ThunderMusic | last post by:
Hi, I have a page where I have an input type=file control and many other controls... the thing is, when I cause a postback by any control on the page, if a file is selected in the input...
5
by: plumba | last post by:
Ok, another query.... I have a checkbox at the bottom of my form which when checked unhides a <div> block which displays the submit button. The problem I have is when the clear form button is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.